问题描述
我当然希望我错过了一些东西,因为我不明白为什么这样做会有所作为。我有一个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.png
和 testImage2.png
保存到照片,然后通过电子邮件发回给自己,以<$返回c $ c> IMG_XXXX.jpg 和 IMG_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中的理论部分正确,但保存Photo时转换不会发生,看起来就像是在飞行中。内部照片存储原始图像扩展(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.
BTW,如果您的目的是出于其他原因(例如上传到服务器,为图像获取 NSData
)发送电子邮件等),我建议反对 UIImageJPEGRepresentation
和 UIImagePNGRepresentation
。他们丢失了元数据,可以使资产更大,如果你使用质量因子小于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.
相反,我建议去返回并从Photos框架中获取原始资产。因此,在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图像的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!