Stack A scroll view onto UIImagePickerController - iphone-sdk-3.0

I'd like to first say I'm a little new to anything but tutorial examples, so sorry if this is a daft question.
I have modified some sample code ever so slightly so that instead of closing a UIImagePickerController once the user has picked an image it processes it in the background.
This seems to work quite well, but once the user has picked all of the images they want to process the only button they're presented with is "Cancel".
I was hoping to change the code so that it adds the selected image to a scrolling view at the bottom of the image picker, but can't find a way to stack or overlay anything onto the UIImagePickerController view.
The code I have so far is below:
-(IBAction)LoadPhotoAlbums:(id)sender
{
NSLog(#"LoadPhotoAlbums");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
return;
}
if(photoPicker == NULL) {
photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.allowsImageEditing = NO;
}
photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:photoPicker animated:YES];
[pool release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if([[info objectForKey:#"UIImagePickerControllerMediaType"] isEqualToString:K_UI_TYPE_IMAGE])
{
NSLog(#"Image Selected");
selectedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"ddMMMyyyyHHmmss"];
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
NSData *imageData = UIImageJPEGRepresentation(selectedImage,0.9);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
selectedImagePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpg", date]];
NSLog(#"Image path is %#", selectedImagePath);
if([imageData writeToFile:selectedImagePath atomically:YES])
NSLog(#"Write Successful");
}
//[picker dismissModalViewControllerAnimated:YES];
//add to listofimages view.
[self uploadSelectedImage];
}

Related

Remove all controls (subviews) in UITabBarViewController

I'm fairly new to ios programming and I encountered an issue lately. I'm trying to navigate to a selected tab of a tabbarviewcontroller programmatically. Upon entering that selected tab, remove all controls in it and repopulate it with different controls.
I managed to navigate to that selected tab programmatically, however, i'm unable to remove all the subviews.
Below are snippet of my codes..
- (IBAction)btnSave:(id)sender {
orthoPaedicInjuryTabBarViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"OrthoInjuryTabBarVC"];
orthoPaedicInjuryTabBarViewController.selectedIndex = 2;
[self presentViewController:orthoPaedicInjuryTabBarViewController animated:YES completion:nil];
self.alert = [[UIAlertView alloc] initWithTitle:#"Empty Field, Step3"
message: #"Please fill up the frequency of exercises"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[self.alert show];
}
- (void)viewWillAppear:(BOOL)animated {
Store *MyStore = [Store SharedStore];
OrthoInjury *orthoInjury = MyStore.orthoInjury;
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd-MM-yyyy"];
NSString *Dob = [format stringFromDate:orthoInjury.dateOfInjury];
self.lblDateOfInjury.text= Dob;
// Do any additional setup after loading the view, typically from a nib.
self.typeOfExercisesList = [Utility GetTypesOfExercisesList];
self.pickerViewTypesOfexercises.dataSource = self;
self.pickerViewTypesOfexercises.delegate = self;
TherapyPhases *therapyPhases = MyStore.therapyPhases;
self.tbxNumberOfWeeks.text = #(therapyPhases.durationWeeks).stringValue;
if([[orthoInjury.natureOfInjury uppercaseString] isEqualToString:[#"Repetitive Strain Injury" uppercaseString]])
{
NSArray *subArray = [self.view subviews];
if([subArray count] != 0) {
for(int i = 1 ; i < [subArray count] ; i++) {
[[subArray objectAtIndex:i] removeFromSuperview];
/*if ([[subArray objectAtIndex:i] isKindOfClass:[UILabel class]])
{
NSLog(#"%#",[[subArray objectAtIndex:i] text]);
}*/
}
}
}
}
Try below for removing the subviews:-
NSMutableArray *subVws=[self.tabBarController.view.subviews mutableCopy];
[subVws performSelector:#selector(removeFromSuperview)];

How do I get GPS to get my co-ordinates every time I click the button?

I'm creating an app that allows a user to take a picture and receive GPS co-ordinates that coincide with the pictures location, displayed in a label in the cameraView. The picture, time/date and GPS location is then transferred to another view for the user to put a title and description and allows them to save the event. A pop-up alerts them it was saved, and when they click save, the view transfers back to the camera view, where the fields are now clear and ready for the next picture.
My issue is that when the user goes back to the camera view, or even stays on the first view and wants to take multiple pictures, no more GPS co-ordinates are put in the label? I can't figure out why. It's like it only can happen once?
I set the cameraView up so when the user clicks the button, coordinates are retrieved every time?
Here is my mainView.h file on the first controller
//labels for the camera view values
IBOutlet UILabel *DateTimeText;
IBOutlet UILabel *LongitudeText;
IBOutlet UILabel *LatitudeText;
NSData *image;
NSData *passBackImage;
Here is the mainView.m file
//When the user hits the "take a picture button"
-(IBAction)onMoreSnapsClick:(id)sender{
UIImagePickerController *pickerControl = [[UIImagePickerController alloc] init];
// If all's good, start up the camera
if (pickerControl != nil)
{
pickerControl.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerControl.delegate = self;
pickerControl.allowsEditing = true;
// Camera turns on
[self presentViewController:pickerControl animated:true completion:nil];
//Retrieve co-ordinates when picture is taken
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//Format date
_date = [[NSDateFormatter alloc] init];
[_date setDateFormat:#"dd-MM-yyyy HH:mm"];
//Date into a string
_dateString = [_date stringFromDate:[NSDate date]];
[self->DateTimeText setText:_dateString];
[_date stringFromDate:[NSDate date]];
//Update the location
[locationManager startUpdatingLocation];
}
}
- (NSString*) getDateTime
{
NSDateFormatter *formatter;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
_dateString = [formatter stringFromDate:[NSDate date]];
return _dateString;
}
//Setting up the delegate for the location finder
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
_snapLatitude =[NSString stringWithFormat:#"%.8f",currentLocation.coordinate.latitude];
_snapLongitude =[NSString stringWithFormat:#"%.8f",currentLocation.coordinate.longitude];
if (currentLocation != nil) {
LongitudeText.text = _snapLatitude;
LatitudeText.text = _snapLongitude;
}
}
//Also attached to my button-clears the fields of the previous picture coords
-(IBAction)onClick:(UIButton*)sender
{
DateTimeText.text = nil;
LongitudeText.text =nil;
LatitudeText.text = nil;
_cameraMainView = nil;
}
//Creating the objects for the images
- (void)imagePickerController:(UIImagePickerController *)pickerControl
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//When finished, access the stored images
_cameraMainView = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
image = UIImagePNGRepresentation(_cameraMainView);
// if everything's okay......
if (_cameraMainView != nil)
// assign the images to the properties
{
_imageView.image = _cameraMainView;
// allow editing and create the delegate
pickerControl.delegate = self;
pickerControl.allowsEditing = true;
}
[pickerControl dismissViewControllerAnimated:true completion:nil];
}
-(IBAction)snapsDetails:(id)sender
{
[self performSegueWithIdentifier:#"snapSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"snapSegue"])
{
SnapAddTextViewController *toSnapView = segue.destinationViewController;
toSnapView.timeDate = _dateString;
toSnapView.snapViewLongitude = _snapLongitude;
toSnapView.snapViewLatitude =_snapLatitude;
toSnapView.snapActualImage =[[UIImage alloc] initWithData:image];
}
}
Can someone tell me why it will only takes one set of coordinates with the first picture taken and none after that? I am still in school and appreciate the leniency on code structure and neatness.I usually pretty it up as things fall together correctly, thanks.a

My image from Parse.com isn't showing.. Using UIImage & UIImageView with PFFile

I have a table on Parse.com that allows me to store Event information. Image, title, date and description. I am trying to obtain the image and assign it to a UIImageView within a custom cell that I have developed... The data loads fine, and the custom cell works, but the image doesn't ever update... The logic in my code seems legit, but then again, I am new... So any help is appreciated.
The problematic code is right after where I have the comment "//--- Event Image"
I am simply trying to figure out why my image isn't updating.
Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = #"eventCell";
VFTEventCell *cell = (VFTEventCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"EventTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//--- Event Title
cell.title.text = [object objectForKey:#"title"];
//--- Event Date
NSDate *eventDate = [object objectForKey:#"date"];
if(eventDate != NULL)
{
NSDate *date = [object objectForKey:#"date"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:date];
cell.date.text = dateString;
}
//--- Event Image
PFFile *eventImage = [object objectForKey:#"image"];
if(eventImage != NULL)
{
[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];
cell.image = thumbnailImageView;
}];
}
return cell;
}
You're trying to set this in a background thread. You can only access UIKit from the main thread.
[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = thumbnailImage;
});
}];
Note: Consider using PFImageView.
I figured out my problem shortly after my post... For anyone who gets stuck. I simply removed the creation of a UIIMageView. Makes sense considering in my XIB file, I am already creating the imageView... But, I set that object using setImage.
here is the code.
//--- Event Image
PFFile *eventImage = [object objectForKey:#"image"];
if(eventImage != NULL)
{
[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
{
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
[cell.image setImage:thumbnailImage];
}];
}

XCode - Display/delete files from NSDocumentDirectory

My application has a 'Browse' button with these codes that allows user to browse the iPad's photo gallery, select a photo and store it into the application using NSDocumentDirectory.
- (IBAction) BrowsePhoto:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:CGRectMake(200,200,-100,-100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
[self.popoverController dismissPopoverAnimated:YES];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:#"SavedImage.png"];
UIImage *image = imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
Now i want to include a 'Display' button which displays all the photos from the NSDocumentDirectory in a new view. Was thinking of displaying it in thumbnails and also when an image is tapped, it will have a pop up asking the user to confirm if he/she wants to delete the selected photo. If yes, the photo will be removed from the NSDocumentDirectory.
Is it possible to do this? If it is, mind telling me how to do it and share some sample codes? I'm quite lost as i'm still quite new to programming.
Everything you need to do with files is implemented in system class called NSFileManager.
It's not complicated. Try to read the documentation and google some examples. There is a lot of them, e.g. http://www.theappcodeblog.com/2011/04/04/nsfilemanager-tutorial-create-read-and-delete-a-file/

UIViewController presents a UINavigationController - no Nav Bar?

I have an UIViewController with a table in it. Tapping a blue disclosure icon makes the view controller present a UINavigationController. This works fine and does make the navigation controller show up, but without a navigation bar, as below:
How would I go about making that magical Navbar showing up again? I really need it to present a map in the nav controller, and right now, that does not work.
EDIT: Here's the code I use to show it:
// scanned recently table view thing
- (void)tableView:(UITableView *)tableVieww didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableVieww deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.section == 0) {
NSString *info = [[scannedBackups objectAtIndex:indexPath.row] objectForKey:#"data"];
NSArray *items = [info componentsSeparatedByString:#"&"];
scanCodeViewCohntroller.dict = [[NSMutableDictionary alloc] init];
int o;
for (o = 0; o < [items count]; o++) {
NSArray *secondItems = [[items objectAtIndex:o] componentsSeparatedByString:#"="];
[scanCodeViewCohntroller.dict setObject:[secondItems objectAtIndex:1] forKey:[secondItems objectAtIndex:0]];
}
/*NSError *err;
scanCodeViewCohntroller.dict = [NSPropertyListSerialization propertyListWithData:[foo dataUsingEncoding:NSUnicodeStringEncoding] options:NSPropertyListMutableContainersAndLeaves format:kCFPropertyListXMLFormat_v1_0 error:&err];
if(err != nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Corrupted Data" message:[NSString stringWithFormat:#"Your Scanned Backup Data Store is corrupted. It will now be erased. %#", [err localizedDescription]] delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
scannedBackups = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:#"%#/scannedCodes.plist", documentsDirectory];
[scannedBackups writeToFile:fullFileName atomically:NO];
[tableView reloadData];
}*/
[scanCodeViewCohntroller.dict setObject:[[scannedBackups objectAtIndex:indexPath.row] objectForKey:#"date"] forKey:#"date"];
[scanCodeViewCohntroller initalizeData];
[self presentModalViewController:scanCodeViewCohntroller animated:YES];
}
}
You need to get the navigation controller and invoke setNavigationBarHidden:animated:. So, try:
[detailController.navigationController setNavigationBarHidden:NO animated:NO];
Edit:
Based on your edit, I would suggest replacing the final line:
[self presentModalViewController:scanCodeViewCohntroller animated:YES];
with:
[self.navigationController pushViewController:scanCodeViewCohntroller animated:YES];