【发布时间】:2016-09-06 04:14:18
【问题描述】:
我想从路径中使用 imageWithContentsOfFile 获取图像,但它返回 nil。然后我尝试 imageWithData,它返回 nil,但数据不是 nil。请帮帮我,非常感谢! 像“/var/mobile/Containers/Data/Application/F865E931-B673-4295-8AC3-B5C3560A10D6/Library/Caches/pic/0513041abff0edbb0e909a1364abe2ed.jpg”这样的路径 对了,模拟器上不会出现这种情况,只有真机上才返回nil case。
NSString * strDestinationPath = [[mainKeepData getPicturesPath] stringByAppendingString:imageName];
NSFileManager * fileManager = [NSFileManager defaultManager];
BOOL bl1 = [fileManager fileExistsAtPath:strDestinationPath];
if (bl1 == NO)
{
[request setDownloadDestinationPath:strDestinationPath];
[request setCompletionBlock:^(void){
UIImage * image=[UIImage imageWithContentsOfFile:strDestinationPath];
[PictureDict setValue:image forKey:strGUID];
[self.delegate DownloadPictureIsOver:YES RequestID:RequetID];
}];
[request setUserInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@",strGUID] forKey:@"name"]];
[networkQueue addOperation:request];
}
else
{
NSData *imageData = [NSData dataWithContentsOfFile:strDestinationPath];
UIImage *image = [UIImage imageWithData:imageData];
[PictureDict setValue:image forKey:strGUID];
}
获取路径函数
+(NSString *)getPicturesPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
NSString *filePath = [cachesDir stringByAppendingPathComponent:@"/pic/"];
filePath=[filePath stringByAppendingString:@"/"];
return filePath;
}
【问题讨论】:
-
如何获取 strDestinationPath ?
-
你检查过,路径引用的数据是否是合法的jpeg?
-
路径和图片可用
-
只有少数案例返回 nil 不是全部
-
我可以在上面的代码中知道您的要求是什么吗?根据您的代码,我认为您正在从服务器下载多个图像并存储在本地缓存中吗?
标签: ios objective-c uiimage