【发布时间】:2013-11-09 08:35:53
【问题描述】:
我想将相机拍摄的照片显示到另一个 UIViewController 中。 我尝试了这段代码 sn-p ,但我认为我做错了,因为照片没有在第二个视图中显示:
- (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");
}
}
但是当我按下选择第二个视图控制器时没有出现。
【问题讨论】:
-
svc.myImag 是如何设置的?它应该是一个图像视图,它是第二个视图控制器的视图之一的子视图。
-
因为您在推送包含图像的视图控制器之前将图像分配给图像视图。在 mainViewController 类中创建一个 UIImage 属性,并在推送视图控制器之前将该属性设置为 imageToSave,然后将该图像分配给图像视图
-
传递 UIImage 对象而不是从这里设置。您必须在第二个 VC 中的 UIImageView 上设置传递的 UIImage 对象。
标签: ios objective-c uiimageview uiimagepickercontroller