我想截取 iPhone 应用程序视图的屏幕截图并将图像保存到给定位置。
我下面的代码将图像保存到照片库,但我想将其保存到其他给定位置。可以这样做吗?请帮我起来。
我的代码在这里:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
您可以使用 NSData 来存储图像数据。
要保存图像:
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(myImage)];
[imageData writeToFile:imagePath atomically:YES];
要检索图像:
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];
更新#1:示例(我现在无法测试该示例,但它应该以这种方式工作):
UIGraphicsBeginImageContext(self.view.bounds.size);
self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
// Get the location of the Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
NSString *imagePath = [paths objectAtIndex:0];
NSString *filename = @"test.png" ;
NSString *filepath = [NSString stringWithFormat:@"%@/%@", imagePath, filename];
UIGraphicsEndImageContext();
// Save the image
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(myImage)];
[imageData writeToFile:filepath atomically:YES];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)