iphone - 如何在 iOS 中获取保存的图像文件路径

标签 iphone ios uiimagepickercontroller photo-gallery

您好,我正在开发一个应用程序,其中有一个选项可以从 iOS 照片库中挑选照片。我必须将选定的照片上传到服务器。要上传图像,我正在使用 Kaltura SDK,所以我无法直接将 UIImage 发送到服务器。我需要发送保存的图像文件路径

为此我找到了一个解决方案,那就是将选定的照片保存在特定的文件路径中,然后我将使用该文件路径。但问题是我不希望在手机内存中再次保存照片,因为这些照片已经存储在特定路径中。

那么有没有其他选项可以在不再次保存的情况下检索保存的图像文件路径?

最佳答案

试试这个:

- (void)saveImage: (UIImage*)image
{
    if (image != nil)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"test.png" ];
        NSData* data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
    } 
}

- (UIImage*)loadImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                  @"test.png" ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    [self sendAction:path];
    return image;
}

关于iphone - 如何在 iOS 中获取保存的图像文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16216770/

相关文章:

ios - 如何从实时照片中提取图像?

ios - 暂停后恢复计时器?

ios - 没有 SubView iOS 的动画 MainView

ios - 如何在 UIImagePicker 中显示最近的照片?

ios - 在 UIImagePickerController 中启用相机胶卷缩略图

iphone - 滚动时确定 UITableViewCell 的坐标

iphone - 将大量数据从 iPad 转换/上传到 Dropbox

iphone - 游戏中心登录锁定横向仅在 iOS 6 中

ios - 如何通过 podfile 共享两个目标中的一些 pod

ios - info[UIImagePickerController.InfoKey.mediaURL] 有时为 nil