[英]Setting UIImageView image from saved screenshot working in iOS 8 but not iOS 7
我制作了一個將設備屏幕截圖保存到應用程序“文檔”目錄的應用程序。
然后,此UIImageView讀取保存的屏幕快照圖像並顯示它。 到目前為止一切都很好。 但是請稍等一下,這在iOS 8上絕對可以正常工作,但是在iOS 7上(模擬器和設備上)完全不顯示任何內容。
因此,我進行了調查,發現iOS 8將圖像保存到以下目錄(OS X上的Simulator):
/用戶/ SunburstEnzo /庫/開發商/ CoreSimulator /設備/ AE8252CE-38C1-4D81-BA87-34F50E743AEC /數據/容器/數據/應用/ 34562F4C-5DED-4F1C-9CB6-92948B6C5F89 /文檔/ 2014-11-26-11 -12-24.png
iOS 7將其保存到此處:
/用戶/ SunburstEnzo /庫/開發商/ CoreSimulator /設備/ CE8E7B90-F00F-441B-8E70-17E968AB7AF7 /數據/應用/ 994CD541-2730-4E32-93CD-23B9F996C2E3 /文檔/ 2014-11-26-11-12-51巴紐
我知道這與它有關,但是當我引用它時,它可以看到很好的圖像,將它放到屏幕上只是一個問題。 而且我知道我所做的工作(在iOS 8上)可以正常工作,因此代碼很好,但是我需要針對此錯誤的解決方法。
此外,無論我遇到什么問題,都應該與該問題有關使用App Containers的轉換類似(注意:保存圖像對我來說很好,只顯示它)- 將圖像保存到文檔目錄不再有效
iOS 7中的tl; dr bug阻止顯示在Documents目錄中保存的圖像,在iOS 8上運行良好,請幫助我解決
我的代碼:
將屏幕快照寫入Documents目錄:UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * sourceImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
//UIImageWriteToSavedPhotosAlbum(sourceImage,nil, nil, nil);
NSData *pngData = UIImagePNGRepresentation(sourceImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
NSString *dateString;
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
dateString = [dateFormatter stringFromDate:now];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",dateString]]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
讀取的代碼:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
directoryContentsPopover = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
NSLog(@"Number of images: %d",directoryContents.count);
if (directoryContentsPopover.count > 0) {
self.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
NSLog(@"At least 1 image; Image location: %@/%@",documentsPath,directoryContentsPopover[0]);
}
****尼克·洛克伍德的答案****
更換
self.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
同
self.mainImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
您正在嘗試使用[UIImage imageNamed:]加載圖像,但是此方法僅適用於應用程序捆綁包或XCAssets中的圖像,它不接受任意文件路徑。
請改用[UIImage imageWithContentsOfFile:]。
如果圖像保存在ios8上,那么它也已保存在ios7中,您將無法正確設置文檔目錄的路徑ios8和ios7的文檔目錄路徑均不同
ios7文檔目錄的路徑:/ Users / ********** / Library / Application Support / iPhone Simulator
嘗試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.