将图像保存到给定位置 [英] Saving images to a given location

查看:86
本文介绍了将图像保存到给定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想截取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();
//获取Documents目录的位置
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * imagePath = [paths objectAtIndex:0];
NSString * filename = @test.png;
NSString * filepath = [NSString stringWithFormat:@%@ /%@,imagePath,filename];
UIGraphicsEndImageContext();
//保存图像
NSData * imageData = [NSData dataWithData:UIImagePNGRepresentation(myImage)];
[imageData writeToFile:filepath atomically:YES];


I want to take screenshots of the iPhone app view and save the images to a given location.

My code below saves the images to the photo library, but I want to save it to some other given location. Is it possible to do so? Please help me up.

My code is here:

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

解决方案

You can use NSData to store the image data.

To save the image:

 NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(myImage)];  
[imageData writeToFile:imagePath atomically:YES];

To retrieve the image:

 UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];

Update #1: Example (I can't test the example right now but it should work this way):

 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];

这篇关于将图像保存到给定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆