将图像保存到给定位置

时间:2010-11-09 05:20:52

标签: iphone iphone-sdk-3.0

我想截取iPhone应用视图的屏幕截图并将图像保存到指定位置。

我的代码将图像保存到照片库,但我想将其保存到其他指定位置。有可能这样做吗?请帮助我。

我的代码在这里:

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

1 个答案:

答案 0 :(得分:3)

您可以使用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];