2010-11-09 92 views
0

我想截取iPhone應用程序視圖並將圖像保存到指定位置。將圖像保存到指定位置

我的代碼將圖像保存到照片庫,但我想將它保存到其他給定的位置。是否有可能這樣做?請幫助我。

我的代碼是在這裏:

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

回答

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

你能否提供我一個例子來嘗試我的代碼...我的意思是,當我嘗試你的代碼與我的uiimage它不工作...請告訴我如何去做 – user198725878 2010-11-09 06:03:17

+0

我已經在你的代碼中添加了一個例子。但是,我現在無法測試代碼。但我很確定它應該以這種方式工作。 – 2010-11-09 06:12:16

+0

你能告訴我...在運行你的代碼時圖像被保存在哪裏...我是新的iPhone開發 – user198725878 2010-11-09 06:38:11

相關問題