2012-10-18 1 views
1

분명히 CGImages를 사용하여 kCGImagePropertyPNGXPixelsPerMeter 및 kCGImagePropertyPNGYPixelsPerMeter 속성을 이미지에 추가하면 png의 pHYs 청크에 추가됩니다. 그러나 UIImageWriteToSavedPhotosAlbum는 CGImage를 직접 수락하지 않습니다.iOS : 디스크에서 PNG를로드하고 PixelPerMeter 속성을 추가 한 다음 사진 앨범에 저장하십시오.

이미지를 UIImage로로드 한 다음 CGImage를 만들고 속성을 추가 한 다음 해당 데이터에서 새 UIImage를 만들고 사진 앨범에 저장하려고했습니다.

사진이 PPM 설정없이 사진 앨범에 기록됩니다. MacOS의 미리보기 앱에서 확인하거나 ImageMagick의 identify을 사용하여 확인할 수 있습니다.

ObjC는 내 도메인이 아니므로 여기에 비슷한 stackoverflow 질문에서 함께 자갈을 채웠습니다.

UIImage *uiImage = [UIImage imageWithContentsOfFile:path]; 

NSDictionary* properties = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:5905], (NSString *)kCGImagePropertyPNGXPixelsPerMeter, /* this doesn't work */ 
           [NSNumber numberWithInt:5905], (NSString *)kCGImagePropertyPNGYPixelsPerMeter, /* this doesn't work */ 
           @"This works when saved to disk.", (NSString *)kCGImagePropertyPNGDescription, 
           nil],(NSString*) kCGImagePropertyPNGDictionary, 
          nil]; 

NSMutableData* imageData = [NSMutableData data]; 
CGImageDestinationRef imageDest = CGImageDestinationCreateWithData((CFMutableDataRef) imageData, kUTTypePNG, 1, NULL); 

CGImageDestinationAddImage(imageDest, uiImage.CGImage, (CFDictionaryRef) properties); 
CGImageDestinationSetProperties(imageDest, (CFDictionaryRef) properties); /* I'm setting the properties twice because I was going crazy */ 
CGImageDestinationFinalize(imageDest); 

UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:imageData], nil, NULL, NULL); 

// This is a test to see if the data is getting stored at all. 
CGImageDestinationRef diskDest = CGImageDestinationCreateWithURL(
    (CFURLRef)[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@.dpi.png",path]], kUTTypePNG, 1, NULL); 

CGImageDestinationAddImage(diskDest, uiImage.CGImage, (CFDictionaryRef) properties); 
CGImageDestinationSetProperties(diskDest, (CFDictionaryRef) properties); 
CGImageDestinationFinalize(diskDest); 

업데이트 : 코드를 약간 수정하고 간소화했습니다. 이미지를 사진 앨범 및 디스크에 계속 저장하지만 미터당 픽셀 값은 저장되지 않습니다. 설명 블록은 디스크에 저장되지만 사진 앨범에 쓰여질 때 제거됩니다.

업데이트 2 : 파일을 jpeg로 저장하고 TIFF X/Y Resolution 태그를 추가하면 디스크에 저장된 버전을 72 이외의 PPI 값을 저장할 수있게되었습니다.이 정보는 여전히 제거됩니다. 사진 앨범에 저장할 때 꺼집니다.

iOS 카메라 앱을 통해 찍은 사진과 Instagram을 통해 찍은 사진과 내 코드로 저장 한 사진을 비교하면 카메라 앱 버전에 TIFF/EXIF ​​태그가 많으며 Instagram과 MyTube가 동일하다는 것을 알았습니다. 제한된 태그 집합. 이것은 iOS가 이러한 태그를 의도적으로 제거한다는 결론에 이르게합니다. 결정적인 대답은 나에게 밤에 잠을 자도록 도와 줄 것입니다.

+0

이미지의 속성을 확인하기 위해 이미지를 다시 가져다 봤습니까? 함수가 값을 설정했으나 변경 사항을 제대로 적용하지 않았을 수 있습니다. 네가 가진 모든 것은 괜찮아 보인다. 캐스팅이 많이 발생하여 다른 객체 유형과의 호환성 문제 일 수 있으며 속성을 실제로 설정할 수 없습니다. 나는 당신의 질문을 본 이후로'CGImageDestination'을보고 있었기 때문에 나는 확실히 말할 수 없다. – RileyE

+0

방금 ​​CGImageDestinationCreateWithURL을 사용하여 디스크에 이미지 저장을 시도했지만 PPM이 설정되지 않았습니다. 그래서 속성을 적용하는 방법에 뭔가 잘못된 것이 있어야합니까? – Calvin

답변

0

답변은 UIImageWriteToSavedPhotos 앨범을 사용하지 마십시오.

ALAssetsLibrary 방법 있다 [writeImageDataToSavedPhotosAlbum : 메타 데이터 completionBlock :]

메타 데이터 CGImageDestinationSetProperties에 전달 된 것과 같은 형식의 사전이다. PNG 및 JFIF 밀도 설정이 손상되었을 수 있습니다. kCGImagePropertyTIFFXResolution을 사용하고 있습니다.