ios - 将图像文件从 Parse 保存到 iPhone

标签 ios objective-c uiimage parse-platform

我有一个名为 data 的解析类,其中包含图像。我的 DetailViewController 包含一个保存来自 data 类的图像的 PFImageView 和一个名为 saveButton< 的 UIButton/

我想将该图像保存到用户 iPhone 上的新文件夹中。我找到了一些相关的答案,但无法成功利用它们。

@property (nonatomic, strong) PFFile *image;
@property (nonatomic, strong) UIImage *imageToSave;

- (void) getImageObject {
    // I'm getting the image here, that I want to download and save to the device
    PFQuery *queryObject = [PFQuery queryWithClassName:@"data"];
    [queryObject whereKey:@"objectId" equalTo:self.objectIdent];
    [queryObject getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

        PFFile *file = [object objectForKey:@"img"];
        // load to the PFImageView
        self.bigImage.file = file;
        [self.bigImage loadInBackground];

        self.image = file;        
    }];
}
// Download and save image to new folder
- (void) getImageFromParse {

    [self.image getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            UIImage *image = [UIImage imageWithData:data];

            NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
            // New Folder is your folder name
            NSError *error = nil;
            if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
                [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

            NSString *fileName = [stringPath stringByAppendingFormat:@"/image.png"];
            NSData *data = UIImagePNGRepresentation(image);
            [data writeToFile:fileName atomically:YES];
            NSLog(@"dev log 2");  
        }
    }];
}
- (IBAction)saveButton:(id)sender {

    [self getImageFromParse];
}

此代码不会产生崩溃,只是什么也没有发生(开发日志 2 出现在控制台中),所以我不知道为什么不运行它或在出错时崩溃。我真的很感激任何可以帮助我解决这个问题的指导或建议。

几乎可以工作的版本,基于 walle84 的回答:(唯一的问题是,它将图像保存到相机胶卷而不是新文件夹)

  [self.image getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
      if (!error) {
        UIImage *image = [UIImage imageWithData:data];

        //Saving to app folder
        NSData *webData = UIImagePNGRepresentation(image);
        [webData writeToFile:@"my new folders name" atomically:YES];
        //imagePath would your app folder
        //OR if album of iphone then below
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)     [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
            if (!error) {

            }
                // Success
                 }];
}];

最佳答案

您可以使用以下代码从解析中获取图像。

 PFQuery *query = [PFQuery queryWithClassName:@"Your_Class_Name"];
 [query getObjectInBackgroundWithId:@"OBJECT_ID" block:^(PFObject *pfObj, NSError *error)
 {
  if (!error) {
       PFFile *imageFile = [pfObj objectForKey:@"image"];
       [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
          if (!error) {
              UIImage *image = [UIImage imageWithData:data];  //Got your image

         //Saving to app folder 
              NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
              NSString *documentsPath = [paths objectAtIndex:0]; 
              NSString *imagePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name to document directory
             NSData *webData = UIImagePNGRepresentation(image);
             [webData writeToFile:imagePath atomically:YES];    //imagePath would your app folder
         //OR if album of iphone then below
             ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
             [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)     [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
                   if (!error)
                     // Success
            }];

         //Best way to create your own folder in album of iphone/ipad.
           [libraryFolder addAssetsGroupAlbumWithName:@"YOUR_ALBUM_NAME" resultBlock:^(ALAssetsGroup *group) 
           {
              NSLog(@"Added folder");
           }failureBlock:^(NSError *error) 
           {
              NSLog(@"Error");
           }];
       }
    }
 }];

你也可以看看 documentation .

关于ios - 将图像文件从 Parse 保存到 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25733177/

相关文章:

ios - Swift:意外放大广告

iphone - 如何根据ios sdk中放置的uiimage的坐标获取 View 的坐标?

ios - 单击按钮时向图像添加阴影 - iOS Swift

objective-c - 应用图标问题上的远程通知角标(Badge)

iphone - 我可以在 UIScrollView 中禁用橡皮筋惯性滚动吗

ios - 场景数据错误 : Type of expression is ambiguous without more context

ios - 查找索引路径的编号

objective-c - 适用于 iPad 的手指绘画/绘画应用程序示例?

ios - 如何隐藏/禁用/删除导航栏后退按钮或隐藏苹果 watch 的状态栏?

ios - 根据 UIView/CGRect 的大小将图像裁剪为正方形