どうやらCGImagesを使用すると、kCGImagePropertyPNGXPixelsPerMeterおよびkCGImagePropertyPNGYPixelsPerMeterプロパティを画像に追加でき、それらはpngのpHYsチャンクに追加されます。しかし、UIImageWriteToSavedPhotosAlbum は CGImage を直接受け入れません。
イメージを UIImage としてロードし、そこから CGImage を作成し、プロパティを追加してから、そのデータから新しい UIImage を作成し、それをフォト アルバムに保存しようとしました。
画像がフォト アルバムに書き込まれていますが、PPM 設定はありません。これは、MacOS のプレビュー アプリで確認するか、ImageMagick の を使用して確認できidentify
ます。
ObjC は私のドメインではないため、同様のスタックオーバーフローの質問からまとめたものを以下に示します。
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);
更新: コードを少し修正して単純化しました。画像は引き続きフォト アルバムとディスクに保存されますが、1 メートルあたりのピクセル数は保存されません。説明ブロックはディスクに保存されますが、フォト アルバムに書き込まれると削除されたように見えます。
更新 2: ファイルを jpeg として保存し、それに TIFF X/Y 解像度タグを追加することで、バージョンをディスクに保存して、72 以外の PPI 値を保存することができました。フォトアルバム。
iOS カメラ アプリで撮影した写真、Instagram で撮影した写真、および私のコードで保存した写真を比較すると、カメラ アプリのバージョンには大量の TIFF/EXIF タグがあり、Instagram と私のバージョンには同じ限定されたタグ セットがあることがわかりました。 . これは、iOS がこれらのタグを意図的に取り除いているという結論につながります。決定的な答えは、夜に眠るのに役立ちます。