在App中拍摄的照片数据传递 [英] Data passing of photo taken in App

查看:113
本文介绍了在App中拍摄的照片数据传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了代码

  -(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).

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

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

推荐答案

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

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.

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

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

@property (nonatomic, retain) UIImage *theImage;

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

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

#import "CropImageViewController.h"

在imagePickerController中,

In imagePickerController,

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

在prepareForSegue方法中:

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

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

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

这篇关于在App中拍摄的照片数据传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆