ios - 来自存储在 iCloud 上的图像的 CGImage 看起来不对

标签 ios uiimage icloud cgimage

我正在从 CGImage 中提取像素颜色使用 this answer 中描述的代码.

但是,我刚刚意识到,如果我加载在另一台设备上创建的图像,像素值看起来是错误的。第一个明显的问题是 alpha 已经消失了。 CGImageAlphaInfo报告 .noneSkipLast ,但我知道图像是 RGBA。如果我从创建它的同一设备读取它,它看起来很好。第二个问题是有一些渗色,好像图像已调整大小。也许是被压缩或什么的。

这是一个例子:

源图是这个西瓜,12x12 Source image

它是在我的 iPad 上创建的。但是,如果我使用我链接的代码通过 iCloud 将它加载到我的 iPhone 上,我会得到:

Bad sampling

Alpha channel 消失了,颜色渗出。

如果我从同一部 iPhone 使用 Airdrop 将小西瓜发送到我的 Mac,然后再次使用 Airdrop 将其发送回(所以它应该是相同的图像!),现在加载它,我得到正确的图像:

Correct image
(深棕色区域是 alpha 为 0 的地方)

如果您有几个启用了 iCloud 的 iOS 设备,您可以在此应用程序中重现此行为:http://swiftpixels.endavid.com
我在哪里使用该代码来读取像素颜色。

这些图像之间可能有什么区别?如何从 iCloud 读取正确的图像?我应该在 UIImage 中寻找提示吗?而不是 CGImage ?

有什么线索吗?谢谢!

更新

作为引用,我正在使用 UIImagePickerController 读取图像,使用此代码:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            loadImageInMemory(image)
        }
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

    fileprivate func loadImageInMemory(_ image: UIImage) {
       /// skipping some preparation
       guard let cgImage = image.cgImage else {
          return
       }
       /// getImageData is a function like getPixelColor linked above,
       /// but for a Rect 
       self.imageData = cgImage.getImageData(rect, width, height)
    }

我还发现了这个可能相关的问题:UIImagePickerController and iCloud photos

正如 Rob 在下面的评论中建议的那样,我将手机中的照片设置更改为“下载并保留原件”(而不是“优化 iPhone 存储”),这样就解决了问题。所以我想问题是为什么 iCloud 试图压缩一个只有 1,355 字节的 PNG 图像,以及是否可以从 UIImagePickerController 访问原始图像

最佳答案

在设置应用程序中,在“照片”下有一个“优化 iPhone 存储”与“下载并保留原件”选项。后者应该在不以任何方式更改图像的情况下同步照片库。

It seems Airdrop can access the original image, though.



Airdrop 不依赖照片库 iCloud 同步来发送图像。这就是为什么 Assets 可以通过,不变的原因。

How can I make my app access the original as well?



这取决于您是否真的想使用照片库来同步您的 Assets 。这似乎特别真实,因为许多人可能根本不会选择将他们庞大的照片库同步到 iCloud。我不确定你想依赖它。

您可能只想自己使用 CloudKit,而不是依赖照片库。也许您应该在应用程序中有一个选项,以便用户可以选择使用 iCloud 存储,如果是这样,请在您的应用程序中使用 iCloud 存储,而不是依赖照片库。

关于ios - 来自存储在 iCloud 上的图像的 CGImage 看起来不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60254076/

相关文章:

ios - swift 如何使 ui 标签始终为 4 行

ios - 如何在 Storyboard中使用 xib 文件作为自定义 View

ios - 带有 iphone 的 Bot Framework SMS channel - 短信不起作用

core-data - 在设备之间共享 UILocalnotification

objective-c - 如何在 iCloud 中保存 NSData

ios - 在 iOS 上使用 AVCaptureSession 加载摄像头速度很慢 - 如何加快速度?

ios - 将视网膜图像转换为视频 : "CGContextScaleCTM: invalid context 0x0. This is a serious error."

ios - 如何在 Swift 中为 UIImages 创建闪烁的指针效果

ios - 如何在 Swift 中从相机或照片库加载图像

iphone - 将 Sqlite 数据库与 iCloud 同步?