写照片(和其他东西)到磁盘,并在以后在iphone中获取它们 [英] Writing photos (and other things) to disk and getting them later in iphone

查看:165
本文介绍了写照片(和其他东西)到磁盘,并在以后在iphone中获取它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



pre $ NS $ args * ;
NSString * documentsPath = [paths objectAtIndex:0];
NSString * savePath = [documentsPath stringByAppendingPathComponent:@photo];
BOOL result = [data writeToFile:savePath atomically:YES];
if(result)NSLog(@Save of photo success!);

然后,我尝试检索表格视图的单元格图像:

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

是的,我检查以确保[leaf content]与savePath返回相同的NSString。还有什么可能是这个问题? [NSData dataWithContentsOfFile:[leaf content]]; 返回零.... 编辑:我正在原始数据,我这样调用writeToFile:

  NSData * dataToAdd = UIImageJPEGRepresentation(image,1.0); 

感谢您们

解决方案如果你有一个名为 file ,那么你将写入路径 filephoto ,但我想你想要的是像 filephoto.jpg ...也许你需要一个图像文件扩展名?或者路径没有完全展开...即使它是相同的,它可能是你没有给 dataWithContentsOfFile:

一个绝对路径。



这是我的猜测。


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

解决方案

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.

这篇关于写照片(和其他东西)到磁盘,并在以后在iphone中获取它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆