[英]How to save images inside app
嗨,我是iOS目標c的新手。 我正在開發一個提取圖像並將其存儲在應用程序內存中以供參考的應用程序。
隨着ALAasset
的棄用,有人可以解釋什么是存儲圖像的最佳方法。
是使用file還是PHImagemanager
。
如何?
您應該將圖像存儲在document目錄中,例如
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *documentsDirectoryForSaveImages = [docsDir stringByAppendingPathComponent:@"/ProfileImages"];
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectoryForSaveImages withIntermediateDirectories:YES attributes:nil error:nil]; // This creates folder in document directory of name "ProfileImages"
現在,您可以存儲類似的圖像,
NSData *data = UIImageJPEGRepresentation(responseObject, 1.0); //Convert image to data
[data writeToFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:formImage] atomically:NO]; //write that data to path. formImage is string value (name of image. Ideally should use timestamp to make unique naming)
您可以檢索類似的圖像,
UIImage *img = [UIImage imageWithContentsOfFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:formImage]];
希望這會有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.