简体   繁体   English

如何使用UI从UIImagePicker加载图像

[英]How to load image from UIImagePicker with alpha

I am using UIImagePicker to load an image that I have upload to the photo library (on my iPad), however it loads without the alpha channel. 我正在使用UIImagePicker加载已上传到照片库(在我的iPad上)的图像,但是加载时没有alpha通道。 I have tripple checked to make sure the image has one. 我检查了三张,以确保图像中有一张。 Unless it is removed when syncing to itunes? 除非在同步到iTunes时将其删除?

Here is the code I am using for the image picker when it finishes picking: 这是我完成图像拾取器使用的代码:

-(void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    //dissmiss picker
    [imagePopOver dismissPopoverAnimated:YES];

    //get the picker image
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    //create image view for selected image
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
    imageView.center = CGPointMake(512, 384);
    [imageLayer addSubview:imageView];

    //release image and image view
    [image release];
    [imageView release];
}

What am I doing wrong? 我究竟做错了什么? I hope this isn't a 'Doh!' 我希望这不是“ Doh!” moment. 时刻。

UIImagePicker does support images with an alpha channel, as Kangoo states in their own answer. 正如Kangoo在其自己的答案中所述,UIImagePicker确实支持带有Alpha通道的图像。

However, the conditions for it working correctly are more complicated than just downloading an image through browser to photo lib. 但是,使其正常工作的条件比仅通过浏览器将图像下载到照片库要复杂得多。

First of all, it makes most sense to be using iOS's image picker to be picking only from the Camera Roll. 首先,最有意义的是使用iOS的图像选择器仅从“相机胶卷”中进行拾取。 On an iOS device, it's possible to save images from Mail or Safari to the Camera Roll, and other apps that support images with an alpha channel allow you to save to the Camera Roll. 在iOS设备上,可以将邮件或Safari中的图像保存到相机胶卷中,而其他支持带有Alpha通道图像的应用程序也可以将其保存到相机胶卷中。

Images in the rest of the Photos Library, other than the Camera Roll, would have gotten there by iTunes sync. iTunes同步会将照片库其余部分中的图像(除了“相机胶卷”之外的图像)带到那里。 And that's a problem. 那是个问题。 Although iPhoto on OSX does support PNG images with alpha channel, when iTunes sync pushes those images to an iOS device the transparent areas of the image background become opaque white. 尽管OSX上的iPhoto确实支持带有Alpha通道的PNG图像,但是当iTunes sync将这些图像推送到iOS设备时,图像背景的透明区域将变为不透明的白色。

Second, if you configure iOS's image picker to allow editing (square crop, move, scale), then the transparent areas of the image background will become opaque black! 其次,如果您将iOS的图像选择器配置为允许编辑(方形裁剪,移动,缩放),则图像背景的透明区域将变为不透明的黑色!

But if you configure iOS's image picker to NOT allow editing, and you pick an image from the Camera Roll, then the picked image (UIImage) can have an alpha channel. 但是,如果将iOS的图像选择器配置为不允许编辑,并且从“相机胶卷”中选择图像,则所选择的图像(UIImage)可以具有Alpha通道。

On the picked UIImage, use CGImageGetAlphaInfo(pickedImage.CGImage) to get the CGImageAlphaInfo, and if that info is not (kCGImageAlphaNone or kCGImageAlphaNoneSkipFirst or kCGImageAlphaNoneSkipLast) then the image has an alpha channel. 在选择的UIImage上,使用CGImageGetAlphaInfo(pickedImage.CGImage)获取CGImageAlphaInfo,如果该信息不是(kCGImageAlphaNone或kCGImageAlphaNoneSkipFirst或kCGImageAlphaNoneSkipLast),则该图像具有Alpha通道。

Never mind. 没关系。 It was as I suspected. 就像我怀疑的那样。 The alpha channel is lost somehow when importing the png through itunes (syncing). 通过iTunes(同步)导入png时,alpha通道会丢失。 Must be converting to 24bit? 必须转换为24bit吗? I uploaded my image to the web and then downloaded it through browser to photo lib and it's fine now. 我将图片上传到网上,然后通过浏览器下载到图片库,现在可以了。

Have you tried calling: 您是否尝试致电:

imageView.backgroundColor = [UIColor clear]

Probably you may need to make sure that imageLayer is transparent, too. 可能您可能还需要确保imageLayer也透明。 I am not sure how this has been implemented. 我不确定这是如何实施的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM