0

我使用了代码

  -(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 点击图片它会更大

4

1 回答 1

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

确保为情节提要中的 segue 标识符设置了一个名称,例如“CropImage”。

于 2013-07-18T03:18:55.343 回答