将照片(和其他内容)写入磁盘并稍后在 iPhone 中获取它们

发布于 2024-08-25 06:25:37 字数 897 浏览 4 评论 0原文

我正在尝试将图像写入磁盘:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2024-09-01 06:25:42

您的新文件名可能不是有效的照片。

如果您有一个名为 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文