Ios imageWithContentsOfFile返回零,imageWithData返回零
我想从路径中获取一个使用imageWithContentsOfFile的图像,但它返回nil。然后我尝试使用imageWithData,它返回nil,但数据不是nil。请帮帮我,非常感谢 路径类似于“/var/mobile/Containers/Data/Application/F865E931-B673-4295-8AC3-B5C3560A10D6/Library/Caches/pic/0513041abff0edbb0e909a1364abe2ed.jpg” 顺便说一下,模拟器上不会出现这种情况,只有在真实机器上返回零的情况Ios imageWithContentsOfFile返回零,imageWithData返回零,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我想从路径中获取一个使用imageWithContentsOfFile的图像,但它返回nil。然后我尝试使用imageWithData,它返回nil,但数据不是nil。请帮帮我,非常感谢 路径类似于“/var/mobile/Containers/Data/Application/F865E931-B673-4295-8AC3-B5C3560A10D6/Library/Caches/pic/0513041abff0edbb0e909a1364abe2ed.jpg” 顺便说一下,模拟器上不会出现这种
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;
}
你可以试试这个,也许对你有帮助
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/pic/"];
return stringPath;
// If you want particular image path
stringPath = [stringPath stringByAppendingPathComponent:@"Your Img Name"];
UIImage *image = [UIImage imageWithContentsOfFile:stringPath];
快乐编码…如何获取strDestinationPath?您是否检查了该路径所引用的数据是否为有效的jpeg?路径和图片有效。少数情况下返回零。我不知道在上述代码中您的要求是什么?根据您的代码,我认为您正在从服务器下载多个图像并存储在本地缓存中,对吗?sailendra kumar,谢谢。但图像仍然为零Sailendra kumar yesl已使用imageWithContentsOfFile/imageWithData,大多数时候返回图像不为零。但是有几张返回的照片是零。好吧。它不是每次都复制的。返回nil的图像,只需验证它是否在文档目录中(在你的图片中),我相信它存在。dataWithContentsOfFile返回的数据不是零。如果我使用模拟器,图像(相同的图像)将显示。