0

我想将相机拍摄的照片显示到另一个 UIViewController 中。我尝试了这个代码片段,但我认为我做错了,因为照片没有显示在第二个视图中:

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

    editedImage = (UIImage *) [info objectForKey:
                               UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:
                                 UIImagePickerControllerOriginalImage];
    imageToSave = (editedImage!=nil ? editedImage : originalImage);


    // Check if the image was captured from the camera
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        // Save the image to the camera roll
        UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
    }

    NSString *docspath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepathJPG = [docspath stringByAppendingPathComponent:@"imagefile.jpg"];

    NSData *data = UIImageJPEGRepresentation(imageToSave, 0.8);
    BOOL result = [data writeToFile:filepathJPG atomically:YES];
    NSLog(@"Saved to %@? %@", filepathJPG, (result? @"YES": @"NO") );

    [picker dismissViewControllerAnimated:YES completion:nil];

    mainViewController *main = [[mainViewController alloc]init];
    [self.navigationController pushViewController:main animated:YES];
    main.myImag.image = imageToSave;
}

更新:

我更改为使用情节提要作为第二个视图:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"idmain"]){
        mainViewController *main = (mainViewController *)[segue destinationViewController];
        main.myImag.image = imageToSave;
    }
}

在 mainViewController 中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *docspath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepathJPG = [docspath stringByAppendingPathComponent:@"imagefile.jpg"];

    UIImage *img = [UIImage imageWithContentsOfFile: filepathJPG];
    if (img != nil) {
        // assign the image to the imageview,
        myImag.image = img;
    }
    else {
        NSLog(@"Image hasn't been created");
    }
}

但是当我按下选择第二个视图控制器时没有出现。

4

2 回答 2

1

您将照片保存在文档目录中,因此您必须从文档目录访问它们

尝试

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString* path = [documentsDirectory stringByAppendingPathComponent:imagefile.jpg];

    UIImage* image = [UIImage imageWithContentsOfFile:path];

通过此代码,您可以从文档目录中获取图像。

于 2013-11-09T08:45:30.370 回答
0

...假设您的imageToSaveUIImage 对象是有效的(首先),

而不是
svc.myImag.image = imageToSave;
这样做

svc.currentImage = imageToSave;
//where currentImage is a UIImage synthesized property in mainViewController class

然后在mainViewController 类的-viewDidLoador中, 设置到UIImageView 对象上。-viewWillAppear
currentImagemyImag

简单地:

//mainViewController
- (void)viewWillAppear {
    ...
    myImag.image = currentImage;
}

原因??... a的-initWithNibName:bundle:
方法 的 iOS 文档指出:UIViewController

… 您指定的 nib 文件不会立即加载。它在第一次访问视图控制器的视图时加载。如果您想在加载 nib 文件后执行额外的初始化,请覆盖该viewDidLoad方法并在那里执行您的任务。…</p>


见类似: UIImageView not display image when property is set too early

于 2013-11-09T09:29:23.920 回答