2014-01-23 4 views
1

각 UIImage에 대한 사용자 정의 이름으로 iPad의 사진 앨범에 UIImage를 저장하려고합니다. 나는 많은 것을 수색하고 나는 문서 디렉토리에 그들을 저장하는 경우에 가능하다는 것을 깨달았다 그러나 사진 앨범에서 아닙니다. 그 해결책을 제안 할 수 있는지 궁금합니다. 사전에 감사합니다, 감사합니다이미지의 사용자 정의 이름을 사용하여 사진 앨범에 UIImage를 저장하는 방법

+0

가능한 복제본. http://stackoverflow.com/questions/18447084/trying-to-save-image-to-photo-album-with-custom-name-in-ios – iCoder

답변

1

나는 다음과 같은 코드를 사용하는 것이 사용자 정의 이름으로 저장하기 :

를 사용하여 사용자 정의 앨범 이름에서 장치의 스크린 샷을 저장하기 위해
// I do this in the didFinishPickingImage:(UIImage *)img method 

// Build NSData in memory from the btnImage... 
NSData* imageData = UIImageJPEGRepresentation(img, 1.0); 

// Save to the default Apple (Camera Roll) folder. 
[imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO]; 

다음 코드 :

- (void)createScreenshotAndSaveInACustomAlbum { 
    DebugLog(@""); 

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(self.view.bounds.size); 
    } 

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    [library addAssetsGroupAlbumWithName:@"My Photo Album" resultBlock:^(ALAssetsGroup *group) { 

     // Checks if group previously created 
     if(group == nil){ 

      // Enumerate albums 
      [library enumerateGroupsWithTypes:ALAssetsGroupAlbum 
           usingBlock:^(ALAssetsGroup *g, BOOL *stop) 
      { 
       // If the album is equal to our album 
       if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) { 

        // Save image 
        [library writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil 
              completionBlock:^(NSURL *assetURL, NSError *error) { 

               // Then get the image asseturl 
               [library assetForURL:assetURL resultBlock:^(ALAsset *asset) { 
                 // Put it into our album 
                 [g addAsset:asset]; 
                } failureBlock:^(NSError *error) { 

                }]; 
              }]; 

       } 
      } failureBlock:^(NSError *error){ }]; 
     } else { 
      // Save image directly to library 
      [library writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil 
            completionBlock:^(NSURL *assetURL, NSError *error) { 

             [library assetForURL:assetURL resultBlock:^(ALAsset *asset) { 
               [group addAsset:asset]; 

              } failureBlock:^(NSError *error) { }]; 
            }]; 
     } 

    } failureBlock:^(NSError *error) { }]; 
} 

희망이 있습니다.

+0

답변을 주셔서 대단히 감사합니다. 사진이 저장된 곳을 찾을 수 없습니다. 나는 그림 b에서 카메라 롤을 확인했지만 찾지 못했습니다. 실제로 카메라를 사용하지는 않지만 스크린 샷을 찍어서 ipad의 사진 앨범에 저장하고 싶습니다. 나는 어떤 도움을 주셔서 감사합니다. 미리 감사드립니다. – user1480179

+0

나는 대답을 편집했다, 희망은 지금 작동한다 :-) –

+0

나는 그림이 어떤 코드로 찍는 이름을 바꿀 수있는 메커니즘이 있다고 들었다. 이 문제를 해결할 수있는 코드를 제게 제공 할 수 있는지 궁금합니다. 미리 감사드립니다. – user1480179

관련 문제