簡體   English   中英

保存UIImage並在UIImageview上重用它

[英]Save a UIImage and reused it on UIImageview

我目前有能力拍攝照片或從相機加載並在我的應用程序中的UIImageView中放置一個圖像我希望能夠有一個按鈕,將保存圖像所以當我重新加載應用程序時,圖像仍然在的UIImageView。

我的雖然是一個按鈕來保存圖像一旦你加載它和在

-(void)viewDidLoad

有代碼加載已保存的圖像,但我不知道如何處理這個。

任何幫助都會很棒。

您可以使用UIImagePNGRepresentationUIImageJPGRepresentation將圖像轉換為NSData ,然后將數據保存到調用NSDatawriteToFile...方法之一的文件中。

然后當您重新啟動應用程序時,可以通過調用[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]來獲取圖像

- 編輯 -

保存圖片

UIImage *image = ...;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];

加載圖片

NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];

我使用了NSCachesDirectory因為我假設您不希望備份此圖像(通過iCloud或iTunes)。 如果你這樣做,你會想要使用NSDocumentDirectory

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM