UIImageView 不显示来自 UIImagePickerController 的 PNG 图像的透明度 [英] UIImageView not showing transparency of PNG Images from UIImagePickerController

查看:31
本文介绍了UIImageView 不显示来自 UIImagePickerController 的 PNG 图像的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当然希望我遗漏了一些东西,因为我不明白为什么会这样.我有一个 PNG 图像,它具有完全透明的背景,因为我想将它覆盖在 UIImageView 内的其他图像上.

I surely hope I am missing something because I do not understand why this is working the way it does. I have a PNG Image, which has a fully transparent background because I want to overlay it on other images inside a UIImageView.

XCode 项目中包含的 PNG 图像都可以正常工作.问题是,当我使用 UIImagePickerController 动态选择这些相同的 PNG 图像,然后将其分配给 UIImageView 时,出于某种非常奇怪的原因,它并没有将其视为具有透明度的 PNG 图像,而是添加了白色背景.

PNG images included in the XCode project all work fine as they should. The problem is when I select these same PNG images on the fly using UIImagePickerController and then assigning it to the UIImageView, for some really bizarre reason, it is not respecting it as a PNG Image with transparency and instead it adding a white background.

任何人以前见过这个,我该如何解决这个问题?

Anyone seen this before and how do I get around this?

* 更新 #1: 我决定尝试一些似乎可以证实我的理论的事情.我决定将我保存到设备中的原始 PNG 图像通过电子邮件发送给自己,然后你瞧,这些图像以 JPG 格式发送给我.在我看来,当您将图像保存到 iPhone 上的照片时,它会将其转换为 JPG,这对我来说相当令人震惊.希望有人有办法解决这个问题.原始图像 testImage1.pngtestImage2.png 保存到 Photos 中,然后通过电子邮件发送给自己,返回为 IMG_XXXX.jpgIMG_XXXX.jpg

* UPDATE #1: I decided to try something that seems to confirm my theory. I decided to email myself the original PNG images I saved to my device and lo and behold, the images came to me as JPG. Seems to me that when you save an image to Photos on iPhone it converts it to JPG, this is rather shocking to me. Hope someone has a way around this. The original images testImage1.png and testImage2.png saved to Photos and then emailed back to myself, returned as IMG_XXXX.jpg and IMG_XXXX.jpg

* 更新 #2: 我一直在玩这个,发现了一些事情,在这个过程中能够回答我自己的问题.(1) 我在 UPDATE #1 中的理论是部分正确的,但是在保存照片时不会发生转换,似乎它是在运行中.照片在内部存储原始图像扩展 (2) 当我在我的 UIImagePickerControllerDelegate 中意识到我正在使用时,我能够验证这一点

* UPDATE #2: I kept playing around we this more and found out a few things and in the process was able to answer my own question. (1) My theory in UPDATE #1 is partially correct, but the conversion does not happen when saving the Photo, seems like it is on the fly. Internally photos stores the original image extension (2) I was able to validate this when I realized in my UIImagePickerControllerDelegate that I was using

let imageData = UIImageJPEGRepresentation(image, 1.0)

而不是这个

let imageData = UIImagePNGRepresentation(image)

当我使用第二行代码时,它正在识别图像的原始透明度属性.

When I used the second line of code, it was recognizing the original transparency properties for the image.

推荐答案

是的,对 UIImageJPEGRepresentation 的调用会将生成的图像转换为不支持透明度的 JPEG.

Yes, the call to UIImageJPEGRepresentation will convert the resulting image into a JPEG, which doesn't support transparency.

顺便说一句,如果您的意图是出于其他原因(例如上传到服务器、发送电子邮件等)为图像获取 NSData,我建议您同时反对 UIImageJPEGRepresentationUIImagePNGRepresentation.它们会丢失元数据,会使资产变大,如果您使用小于 1 的品质因数会导致图像质量下降,等等.

BTW, if your intent is to get the NSData for the image for other reasons (e.g. uploading to server, emailing, etc.), I would recommend against both UIImageJPEGRepresentation and UIImagePNGRepresentation. They lose meta data, can make the asset larger, if suffer some image degradation if you use quality factor of less than 1, etc.

相反,我建议您返回并从照片框架中获取原始资产.因此,在 Swift 3 中:

Instead, I'd recommend going back and get the original asset from the Photos framework. Thus, in Swift 3:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let url = info[UIImagePickerControllerReferenceURL] as? URL {
        let result = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
        if let asset = result.firstObject {
            let manager = PHImageManager.default()
            manager.requestImageData(for: asset, options: nil) { imageData, dataUTI, orientation, info in
                if let fileURL = info!["PHImageFileURLKey"] as? URL {
                    let filename = fileURL.lastPathComponent
                    // use filename here
                }

                // use imageData here
            }
        }
    }

    picker.dismiss(animated: true)
}

如果您也必须支持 iOS 7,则可以使用等效的 ALAssetsLibrary API,但想法是一样的:获取原始资产而不是通过 UIImage.

If you have to support iOS 7, too, you'd use the equivalent ALAssetsLibrary API, but the idea is the same: Get the original asset rather than round-tripping it through a UIImage.

(对于 Swift 2 版本,请参阅此答案的上一版本.)

(For Swift 2 rendition, see previous revision of this answer.)

这篇关于UIImageView 不显示来自 UIImagePickerController 的 PNG 图像的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆