1

显然,使用 CGImages 您可以将 kCGImagePropertyPNGXPixelsPerMeter 和 kCGImagePropertyPNGYPixelsPerMeter 属性添加到图像中,它们将被添加到 png 的 pHYs 块中。但 UIImageWriteToSavedPhotosAlbum 不直接接受 CGImage。

我一直在尝试将图像加载为 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 分辨率标签,我已经能够将版本保存到磁盘以存储 PPI 值而不是 72。保存到时仍然会删除此信息相册。

比较一张通过 iOS 相机应用程序拍摄的照片、一张通过 Instagram 拍摄的照片以及一张由我的代码保存的照片,我注意到相机应用程序版本有大量的 TIFF/EXIF 标签,而 Instagram 和我的照片具有相同的有限标签集. 这使我得出结论,iOS 故意剥离了这些标签。不过,一个明确的答案会帮助我晚上睡觉。

4

1 回答 1

0

答案是不要使用 UIImageWriteToSavedPhotosAlbum。

有一个 ALAssetsLibrary 方法 [writeImageDataToSavedPhotosAlbum: metadata: completionBlock:]

元数据是一个与传递给 CGImageDestinationSetProperties 格式相同的字典。PNG 和 JFIF 密度设置可能被破坏,我正在使用 kCGImagePropertyTIFFXResolution。

于 2012-10-18T17:24:40.047 回答