在App中拍摄的照片的数据传递

时间:2013-07-18 01:49:06

标签: ios image camera

我使用了代码

  -(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];
    }
 }

我在这里使用故事板有问题。我在故事板中有多个视图控制器。我在第一个视图控制器上有相机按钮(用户可以在应用程序中拍摄照片)。但是,我想将用户在第一个视图控制器中拍摄的照片数据自动传递给另一个视图控制器,以便用户可以裁剪它,然后将其保存到“目录或某物”中(尚未决定)。

更新 我现在有四个错误 http://tinypic.com/view.php?pic=15q6y3b&s=5 点击它会更大的图像

1 个答案:

答案 0 :(得分:0)

实际上,您不必将照片直接传递到下一个视图,因为您已经保存了它。您只需从目标视图控制器中的同一文件中读回图像即可。 (如果视图控制器彼此不相近,则此方法更好。)

+ (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];
}

更新:您也可以使用属性传递照片。

在目标视图控制器中(我将其命名为CropImageViewController),在.h中,

@property (nonatomic, retain) UIImage *theImage;

在“getPhoto”视图控制器中,在页面顶部,

#import "CropImageViewController.h"

在imagePickerController中,

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

在prepareForSegue方法中:

- (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];
    }
}

确保设置名称,例如“CropImage”,用于故事板中的segue标识符。