将图像文件从Parse保存到iPhone

时间:2014-09-08 21:10:00

标签: ios objective-c uiimage parse-platform

我有一个名为data的解析类,其中包含图像。我的DetailViewController包含一个PFImageView,其中包含来自data类的图像和一个名为UIButton的{​​{1}}。

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

saveButton

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

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

@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 个答案:

答案 0 :(得分:1)

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

 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

答案 1 :(得分:0)

试试这个:

- (void) getImageFromParse {

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

            ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

    [library saveImage:image toAlbum:@"New Folder" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];
            NSLog(@"dev log 2");  
        }
    }];
}