将照片(和其他内容)写入磁盘并稍后在 iPhone 中获取它们
我正在尝试将图像写入磁盘:
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!");
然后,稍后,我尝试检索表视图单元格图像:
NSData* getPhoto = [NSData dataWithContentsOfFile:[leaf content]];
UIImage* myImage = [UIImage imageWithData:getPhoto];
cell.imageView.image = myImage;
是的,我检查以确保 [leaf content] 返回与 savePath 相同的 NSString。还有什么问题? [NSData dataWithContentsOfFile:[leaf content]];
返回 nil....
编辑:我正在以这种方式制作我称之为 writeToFile 的原始数据:
NSData* dataToAdd = UIImageJPEGRepresentation(image, 1.0);
谢谢大家
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的新文件名可能不是有效的照片。
如果您有一个名为
file
的图像,那么您将写入路径filephoto
,但我认为您想要的是类似filephoto.jpg
>...也许您需要图像文件扩展名?或者路径可能没有完全展开......即使它是相同的,也可能您没有给
dataWithContentsOfFile:
绝对路径。这些是我的猜测。
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 pathfilephoto
, but i think what you want is something likefilephoto.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.