Writing photos (and other things) to disk and getting them later in iphone

我的梦境 提交于 2019-12-08 06:21:05

问题


I'm trying to write an image to disk:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString* savePath = [documentsPath stringByAppendingPathComponent:@"photo"];
    BOOL result = [data writeToFile:savePath atomically:YES];
    if(result) NSLog(@"Save of photo success!");

Then, later, I try to retrieve for a table view cell image:

    NSData* getPhoto = [NSData dataWithContentsOfFile:[leaf content]];
    UIImage* myImage = [UIImage imageWithData:getPhoto];
    cell.imageView.image = myImage;

Yes, I checked to make sure the [leaf content] returns the same NSString as savePath. What else could be the problem? [NSData dataWithContentsOfFile:[leaf content]]; returns nil....

EDIT: I am making the original data that I call writeToFile on this way:

NSData* dataToAdd = UIImageJPEGRepresentation(image, 1.0);

Thanks guys


回答1:


It's possible that your new filename is not a valid photo.

If you have an image named file, then you will be writing to the path filephoto, but i think what you want is something like filephoto.jpg... maybe you need a image file extension?

Or maybe the path isn't fully expanded...even though it is the same, its possible that you aren't giving dataWithContentsOfFile: an absolute path.

Those are my guesses.



来源:https://stackoverflow.com/questions/2453023/writing-photos-and-other-things-to-disk-and-getting-them-later-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!