写照片(和其他东西)到磁盘,并在以后在iphone中获取它们 [英] Writing photos (and other things) to disk and getting them later in 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 $ c的图片,那么你的新文件名不是有效的照片。 $ c>,那么你将写入路径 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屋!