我在UITableView中顯示非常大的圖像,因此我的應用程序在一段時間後崩潰。現在我想調整圖像大小並將其保存到磁盤。有人可以幫助我調整NSData/UIImage的圖像大小並保存到磁盤。從iPhone的UIImage將圖像保存到沙盒中
我得到的代碼來調整圖像從UIImage,所以結果我有我在UIImage對象的圖像調整大小,如何將它保存到iphone沙箱。
謝謝,
我在UITableView中顯示非常大的圖像,因此我的應用程序在一段時間後崩潰。現在我想調整圖像大小並將其保存到磁盤。有人可以幫助我調整NSData/UIImage的圖像大小並保存到磁盤。從iPhone的UIImage將圖像保存到沙盒中
我得到的代碼來調整圖像從UIImage,所以結果我有我在UIImage對象的圖像調整大小,如何將它保存到iphone沙箱。
謝謝,
你在問'我該怎麼寫文件'吧?
我想你可能想寫入Library/Caches目錄中有一些複雜因素,所以當手機備份時你不保存這些文件。
獲取應用程序文件夾的根w/NSHomeDirectory()
,然後附加庫/緩存。
您可以在NSData上的fs w/a方法中編寫一個NSData。如果您有UIImage,則可以使用UIImageJPEGRepresentation()
或UIImagePNGRepresentation
來獲取數據。
NSData *imgData = UIImageJPEGRepresentation(image, 1);
CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imgData, NULL);
NSDictionary *metadata = [(NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[imageMutableData writeToFile:jpgPath atomically:YES];
將您的圖像複製到文件夾中的沙箱名稱Test.jpg
。
如果你想這樣做對PNG,你可以嘗試UIImagePNGRepresentation(image);
這裏是代碼保存到iOS上的文件目錄是從工作中。
// Convert UIImage to JPEG
NSData *imgData = UIImageJPEGRepresentation(image, 1); // 1 is compression quality
// Identify the home directory and file name
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write the file. Choose YES atomically to enforce an all or none write. Use the NO flag if partially written files are okay which can occur in cases of corruption
[imgData writeToFile:jpgPath atomically:YES];
真棒答案,它幫助了我。這是簡單的(一行代碼)再次訪問該數據?或者我應該問一個關於堆棧的問題? – KKendall 2013-05-21 03:21:08
你可能想使用 的NSArray *路徑= NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES); NSString * cachesDirectory = [paths objectAtIndex:0]; 而不是自己附加「Library/Caches」。 – 2010-03-31 12:05:55