开发者

Image not saving in Objective C

I am downloading a file using the following:

NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL HERE"]];

Displaying it with the following:

UIImage *img = [UIImage imageWithData:imgData];

And saving it to the device with the following:

NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pth = [[paths objectAtIndex:0] stringByAppendingPathComponent:self.localFile];    

NSData *d1 = [NSData dataWithData:UIImageJPEGRepresentation(img,1.0f)];
[d1 writeToFile:pth atomically:YES];

Photo displays fine in my UIImageView. But when I reload the application and display the fi开发者_运维百科le, I get the following error:

ImageIO: CGImageRead_mapData 'open' failed '/var/mobile/Applications/422122E1-3244-46CE-BB6C-123C750E2191/Documents/14741078_1.jpg'
         error = 2 (No such file or directory)
ImageIO: <ERROR> JPEGNot a JPEG file: starts with 0xff 0xd9


I have just resolved the same issue.

Don't change your load method.
But save your image like this :

NSError* error = nil;
[UIImageJPEGRepresentation(img, 1.0f) writeToFile:pth options:NSDataWritingAtomic error:&error];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜