我正在截取我的视图并将其存储在文档目录中。但我需要在iphone gallery app中显示存储的图像。我不知道如何将图像传递到图库视图。 帮助我。 谢谢你在... ..
- (IBAction)Screenshot:(id)sender {
CGSize targetImageSize = CGSizeMake(500, 500);
// Check for retina image rendering option
if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(targetImageSize, NO, 0);
else UIGraphicsBeginImageContext(targetImageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// The view to be rendered
[[image layer] renderInContext:context];
// Get the rendered image
UIImage *original_image = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"%@",original_image);
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/screenshots"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
//Create folder
// NSString *documentsDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/myPngFile.png",dataPath];
[UIImagePNGRepresentation(original_image) writeToFile:pngFilePath atomically:YES];
//[_image1 setImage:original_image];
UIGraphicsEndImageContext();
}
答案 0 :(得分:3)
您可以使用此功能:
UIImageWriteToSavedPhotosAlbum(UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo);
如果你想在保存UIImage时收到通知,你只需要completionTarget,completionSelector和contextInfo,否则你可以传入nil。
例如:
-(void)savePhoto {
NSURL *imageURL = receivedURL;
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void) savedPhotoImage:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"This image has been saved to your photo album"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[alert show];
}
答案 1 :(得分:0)
这里我在另一个视图中传递String变量,就像你可以用于图像一样。 对于图像 代码如下:
UserDetailsViewController *userDetailsViewController = [[UserDetailsViewController alloc] initWithNibName:@"UserDetailsViewController" bundle:nil];
userDetailsViewController.img = [UIImage imageNamed:@"abc.png"];
[self.navigationController pushViewController:userDetailsViewController animated:YES];
答案 2 :(得分:0)
如果你需要在iPhone Photo App中看到它,你必须将图像存储在相册中使用此代码
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage *image=//what ever Image you wan tot save and view in photo album of iPhone;
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[SAVEIMAGE imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
// TODO: error handling
} else {
// TODO: success handling
}
}];
答案 3 :(得分:0)
使用以下代码将存储的图像存入图库
UIImageWriteToSavedPhotosAlbum(imgNewImage, nil, nil, nil);
imgNewImage 是您的UIImage对象。