Data passing of photo taken in App

2019-08-30 02:09发布

问题:

I used the code

  -(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil];
[self performSegueWithIdentifier:@"CropImage" sender:sender];
}


- (void) imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info {

//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];
}
}
- (void)imagePickerControllerDidCancel  :(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];

}

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"CropImage"])
{
    CropImageViewController *cropImageViewController = segue.destinationViewController;
    cropImageViewController.theImage = yourImage;
    // Similar to [segue.destinationViewController setTheImage:yourImage];
    }
 }

I have a problem here using the storyboard. I have multiple view controllers in the storyboard. I have the camera button (which the user can take the photo in app) on the first view controller. However I want to pass the data of the photo that the user took in the first view controller to another view controller automatically so that the user can crop it and then save it into a "directory or something"(did not decide yet).

update I am having now four errors currently saying http://tinypic.com/view.php?pic=15q6y3b&s=5 Click the image it will be bigger

回答1:

Actually, you don't have to pass the photo directly to the next view since you had already saved it. You can simply read back the image from the same file in the target view controller. (This method is better if the view controllers are not close to one another.)

+ (UIImage *)readImageFromFile:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
    return [UIImage imageWithData:imageData];
}

UPDATED: You can also pass the photo using property though.

In your target view controller (I will name it CropImageViewController), in .h,

@property (nonatomic, retain) UIImage *theImage;

In the "getPhoto" view controller, on top of the page,

#import "CropImageViewController.h"

In imagePickerController,

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    ...
    // This line should be
    [self performSegueWithIdentifier:@"CropImage" sender:self];
}

In prepareForSegue method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"CropImage"])
    {
        CropImageViewController *cropImageViewController = segue.destinationViewController;
        cropImageViewController.theImage = yourImage;
        // Similar to [segue.destinationViewController setTheImage:yourImage];
    }
}

Make sure you set a name, e.g. "CropImage", for the segue identifier in storyboard.



标签: ios image camera