5

나는 사용자가이 나에게 반환되는 사전 키있는 사진 라이브러리에서 이미지를 선택하도록 UIImagePickerController를를 사용UIImagePickerControlleHow의 결과에서 메타 데이터가 포함 된 JPEG를 얻는 방법은 무엇입니까? 아이폰 OS 4.2에

2011-03-02 13:15:59.518 xxx[15098:307] didFinishPickingMediaWithInfo: 
    info dictionary: { 
    UIImagePickerControllerMediaType = "public.image"; 
    UIImagePickerControllerOriginalImage = "<UIImage: 0x3405d0>"; 
    UIImagePickerControllerReferenceURL = 
     "assets-library://asset/asset.JPG?id=1000000050&ext=JPG"; 
} 

하나 또는 이러한 키의 더 많은 사용을 내가 어떻게 얻을 수 있습니다 이미지 메타 데이터 (예 : 노출 정보 및 GPS 위치 데이터)를 포함하여 내가 어딘가에 업로드하고 메타 데이터를 포함 할 수 있도록하는 JPEG 표현 (벗겨 내지 않음)

워런 버튼의 아주 좋은 답변은 Display image from URL retrieved from ALAsset in iPhone?에서 UIImagePickerControllerReferenceURL 및 ALAssetsLibrary assetForURL 메서드를 사용하여 ALAsset 및 ALAssetRepresentation을 얻는 방법을 참조하십시오. 그렇다면 JPEG에서 모든 메타 데이터를 포함하도록하려면 어떻게해야합니까?

UIImage를 통한 메커니즘이 있습니까?

여기 결론

내가 좀 더 실험을 수행하고 지금은 답을 알고 있다고 생각했던 질문을하기 때문에 ...

답변

7

그 안에 포함 된 메타 데이터와 JPEG을 얻고 싶은 것입니다. 보인다

NSData *imageData = UIImageJPEGRepresentation(self.selectedImage, 0.9); 

이 (당신에게 메타 데이터를 (많이)주지하는 모든 결과는 모든

첫째, 우리는 UIImageJPEGRepresentation 알라를 사용했다 ... 내가 걱정하는 모든입니다 아이폰 OS 4.2에 입수했다 EXIF, GPS 등)을 표시합니다. 충분히 공정하고 나는 그것이 잘 알려져 있다고 생각합니다.

내 테스트에 따르면 이미지 애셋의 "기본 표현"인 JPEG에는 EXIF ​​및 GPS 정보 (처음부터 있다고 가정)가 포함 된 모든 메타 데이터가 포함됩니다. 애셋 URL에서 애셋으로 애셋의 기본 표현 (ALAssetRepresentation)으로 이동 한 다음 getBytes 메소드/메시지를 사용하여 JPEG 이미지의 바이트를 검색하여 해당 이미지를 가져올 수 있습니다. 해당 바이트 스트림에는 앞에서 설명한 메타 데이터가 있습니다.

여기에 내가 사용하는 코드 예가 ​​있습니다. 이미지 용으로 간주되는 애셋 URL을 사용하고 JPEG와 함께 NSData를 반환합니다. 귀하의 사용, 코드의 오류 처리 등과 관련하여주의해야합니다.

/* 
* Example invocation assuming that info is the dictionary returned by 
* didFinishPickingMediaWithInfo (see original SO question where 
* UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=1000000050&ext=JPG"). 
*/ 
[self getJPEGFromAssetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]]; 
// ... 

/* 
* Take Asset URL and set imageJPEG property to NSData containing the 
* associated JPEG, including the metadata we're after. 
*/ 
-(void)getJPEGFromAssetForURL:(NSURL *)url { 
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 
    [assetslibrary assetForURL:url 
     resultBlock: ^(ALAsset *myasset) { 
      ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
#if DEBUG 
      NSLog(@"getJPEGFromAssetForURL: default asset representation for %@: uti: %@ size: %lld url: %@ orientation: %d scale: %f metadata: %@", 
      url, [rep UTI], [rep size], [rep url], [rep orientation], 
      [rep scale], [rep metadata]); 
#endif 

      Byte *buf = malloc([rep size]); // will be freed automatically when associated NSData is deallocated 
      NSError *err = nil; 
      NSUInteger bytes = [rep getBytes:buf fromOffset:0LL 
           length:[rep size] error:&err]; 
      if (err || bytes == 0) { 
       // Are err and bytes == 0 redundant? Doc says 0 return means 
       // error occurred which presumably means NSError is returned. 

       NSLog(@"error from getBytes: %@", err); 
       self.imageJPEG = nil; 
       return; 
      } 
      self.imageJPEG = [NSData dataWithBytesNoCopy:buf length:[rep size] 
            freeWhenDone:YES]; // YES means free malloc'ed buf that backs this when deallocated 
     } 
     failureBlock: ^(NSError *err) { 
      NSLog(@"can't get asset %@: %@", url, err); 
     }]; 
    [assetslibrary release]; 
} 
+0

예고없이 assetLib은 비동기이므로 미리로드하지 않으면 imageJPEG가 nil이 될 수 있습니다. – rnaud

+1

+1. 마침내 메타 데이터가 콘솔에 인쇄 된 것을 볼 수 있습니다. 좋은 대답! – gary