カメラで撮影した写真を別の UIViewController に表示したいと思います。このコード スニペットを試してみましたが、写真が 2 番目のビューに表示されないため、何か間違ったことをしたと思います。
- (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;
}
アップデート:
2 番目のビューにストーリーボードを使用するように変更します。
-(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");
}
}
しかし、選択を押しても、2番目のビューコントローラーは表示されません。