UIImagePicker从webView显示保存的图像

时间:2013-07-06 12:29:59

标签: ios objective-c cocoa-touch

在我的iPad应用中,用户从webView中选择一张图片。保存的图片显示在imagePicker中包含的UIPopOver中。用户可以裁剪图像并调整其大小,完成后,所选图像将显示在UIImageView中。

所有这些都很有效,但要实现它,使用以下代码将从webView中选择的图像加载到用户的库中:

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

理想情况下,我不希望将图像保存到库中,因为应用完成后不会需要或保存图像。如果有帮助,请使用以下代码将webView中的图像放入库中:

UIGraphicsBeginImageContext(webPage.frame.size);
[webPage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil,nil,nil);

1 个答案:

答案 0 :(得分:1)

然后将其保存到NSDocumentsDirectory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
    NSString *picture_information = [docs stringByAppendingFormat:@"/picture_name.png"];

//your code

UIGraphicsBeginImageContext(webPage.frame.size);
[webPage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[UIImagePNGRepresentation(viewImage) writeToFile:picture_information atomically:YES];

//And you retrieve it like this:

 UIImage *img = [UIImage imageWithContentsOfFile:picture_information];

希望这有帮助。