imageWithContentsOfFile返回nil并且imageWithData返回nil

时间:2016-09-06 04:14:18

标签: ios objective-c uiimage

我想从路径中获取图像使用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;
}

1 个答案:

答案 0 :(得分:0)

可以试试这个,可能会帮到你

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];

快乐的编码......