[英]How do I display an alert popup from another Class that is not a UIViewController?
[英]How to display photo into another UIViewController
我想将相机拍摄的照片显示到另一个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");
}
}
但是当我按选择时,第二个视图控制器不会出现。
您要将照片保存在文档目录中,因此必须从文档目录访问它们
尝试
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:imagefile.jpg];
UIImage* image = [UIImage imageWithContentsOfFile:path];
通过此代码,您可以从文档目录中获取图像。
...假设您的imageToSave
UIImage对象有效(首先),
代替
svc.myImag.image = imageToSave;
做这个
svc.currentImage = imageToSave;
//where currentImage is a UIImage synthesized property in mainViewController class
然后在mainViewController类的-viewDidLoad
或-viewWillAppear
中,
将currentImage
设置到myImag
UIImageView对象上。
只是:
//mainViewController
- (void)viewWillAppear {
...
myImag.image = currentImage;
}
原因?? ...
UIViewController
的-initWithNibName:bundle:方法的iOS文档指出:
…您指定的nib文件不会立即加载。 它是在第一次访问视图控制器的视图时加载的。 如果要在加载nib文件后执行其他初始化,请重写
viewDidLoad
方法并在其中执行任务。 ...
看到类似的内容: 属性设置得太早时,UIImageView不显示图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.