2014-09-08 3 views
0

이미지가 들어있는 data이라는 구문 분석 클래스가 있습니다. 내 DetailViewController에는 data 클래스의 이미지를 보유하는 PFImageViewsaveButton이라는 UIButton이 포함되어 있습니다.이미지 파일을 구문 분석에서 iPhone으로 저장

그 이미지를 사용자의 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]; 
} 

이 코드는 단순히 아무것도 (dev에 로그 콘솔에서이 개 나타납니다) 발생하지 않습니다, 충돌이 발생하지 않기 때문에 나는 그것이 잘못된 경우를 실행하거나 충돌하지 않는 이유 어떤 생각이 없습니다. 이 문제를 해결하는 데 도움이되는 지침이나 제안을 정말 고맙게 생각합니다.

거의 작업 버전, 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 
       }]; 
}]; 

답변

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을 살펴볼 수 있습니다.

+0

이미지를 얻었지만 문제는 아닙니다. 문제는 장치에 저장할 수 없다는 것입니다. – rihe

+0

앱의 앨범이나 coredata에 저장 하시겠습니까? – nikhil84

+0

나는 내 게시물을 편집 했으므로 지금 살펴보고 필요한 것이 있으면 알려주십시오. – nikhil84

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"); 
     } 
    }]; 
} 
관련 문제