0

我试图加载一个带有透明背景的PNG文件到一个UIImageView中(如果它很重要,用Objective C,XCode 8.0开发IOS 10.0目标)。 当显示在UIImageView中时,图像的背景颜色显示为黑色(见下文)。iOS Objective C加载一个带有透明背景的PNG文件到一个UIImageView

如何将任意PNG文件加载到UIImageView,使其透明背景被保留?

奇怪的是,我的应用程序资产中有透明背景的PNG图像,并且它们显示在UIImageView中,没有任何额外的混乱。

请注意在下面的代码中,我尝试过的一件事是重建从imagePicker(请参阅imageWithImage)传递的UIImage,为新图像设置新的图形上下文。我曾经在某些帖子中看到过这种方法,它们涉及到模糊的类似问题,但它不起作用(更不用说,似乎愚蠢的IOS会要求它支持加载映像中定义的透明度)。

的PNG图像我与测试是一个标准的PNG32(从互联网上用于测试目的收获 - 注意,我已经尝试了许多其他图片):

enter image description here

左下图显示之前的应用程序屏幕加载上面的图片,右键显示加载的图片: enter image description here

PNG图片文件是通过点击屏幕底部的'select image'打开的imagePicker来选择的。

我的视图控制器代码:

// 
// ViewController.m 
// ImageTest 
// 

#import "ViewController.h" 
#import "MobileCoreServices/MobileCoreServices.h" 


#define FONT_SIZE_BUTTON 32 

@interface ViewController() 

@end 

@implementation ViewController 

UIImageView *myImageView; 
UIImage *myImage; 
UIButton *btnSelect; 

- (void)viewDidLoad { 

    NSLog(@"viewDidLoad"); 

    [super viewDidLoad]; 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    CGFloat btnHeight = 40; 
    CGFloat btnMargin = 10; 
    CGFloat btnWidth = screenRect.size.width - 2*btnMargin; 
    CGFloat btnULY = screenRect.size.height - btnHeight - 2*btnMargin; 

    btnSelect = [[UIButton alloc] initWithFrame:CGRectMake(btnMargin, btnULY, btnWidth, btnHeight)]; 
    [btnSelect.titleLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE_BUTTON]]; 
    [btnSelect setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
    [btnSelect setTitle :@"Select Image" forState:UIControlStateNormal]; 
    [btnSelect.layer setBorderWidth:4.0f]; 
    [btnSelect.layer setBorderColor:[UIColor blueColor].CGColor]; 
    [btnSelect setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
    [btnSelect setTitleColor:[UIColor grayColor] forState:UIControlStateSelected]; 
    [btnSelect setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; 
    btnSelect.backgroundColor = [UIColor whiteColor]; 
    btnSelect.tag = 4200; 
    [btnSelect addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; 
    btnSelect.userInteractionEnabled = true; 

    CGFloat ulx = 10; 
    CGFloat uly = 20; 
    CGFloat width = screenRect.size.width - 2*ulx; 
    CGFloat height = width * 9.0/16.0; 

    CGRect imageFrame = CGRectMake(ulx, uly, width, height); 

    myImageView = [[UIImageView alloc] initWithFrame:imageFrame]; 
    myImageView.alpha = 1.0f; 
    myImageView.backgroundColor = [UIColor lightGrayColor]; 
    myImageView.contentMode = UIViewContentModeScaleAspectFit; 

    myImageView.opaque = false; 
    [[myImageView layer] setOpaque:false]; 

    [[myImageView layer] setBorderWidth:4.0f]; 
    [[myImageView layer] setBorderColor:[UIColor redColor].CGColor]; 

    // Add elements to this view controller 
    [self.view addSubview:myImageView]; 
    [self.view addSubview:btnSelect]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


// Ensure image is opened honoring transparency of PNGs 
- (UIImage *)imageWithImage:(UIImage *)image { 
    CGSize newSize = image.size; 
    CGRect newRect = CGRectMake(0,0,newSize.width, newSize.height); 

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0); 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor clearColor] set]; 
    CGContextFillRect(ctx, newRect); 

    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.Height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
#pragma mark - Select Actions 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- (IBAction)onClick: (UIButton *)btn 
{ 
    NSLog(@"onClick"); 
    // DDLogInfo(@"%@:%@", THIS_FILE, THIS_METHOD); 

    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; 
    [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; 
    picker.mediaTypes = mediaTypes; 

    [self presentViewController:picker animated:YES completion:NULL]; 
} 


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
#pragma mark - UIImagePicker's Delegate 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    NSLog(@"imagePickerController"); 

    UIImage *chosenImage = [self imageWithImage:info[UIImagePickerControllerEditedImage]]; 
// UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
// UIImage *chosenImage = [info[UIImagePickerControllerEditedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
    if (myImageView != nil) 
     myImageView.image = chosenImage; 

    myImage = chosenImage; 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 


@end 

**

与Final修复编辑

**

更新后的应用屏幕的截屏显示问题:

enter image description here

和最终(固定)的代码,显示使用字典的键(UIImagePickerControllerOriginalImage)和字典键(UIImagePickerControllerEditedImage)之间的区别:

// 
// ViewController.m 
// ImageTest 
// 
// Created by Robb Main on 2016-11-21. 
// Copyright © 2016 Robb Main. All rights reserved. 
// 

#import "ViewController.h" 
#import "MobileCoreServices/MobileCoreServices.h" 


#define FONT_SIZE_BUTTON 32 

@interface ViewController() 

@end 

@implementation ViewController 

UIImageView *myImageView1; 
UIImageView *myImageView2; 
UIImageView *myImageView3; 
UIImage *myImage; 
UIButton *btnSelect; 

- (void)viewDidLoad { 

    NSLog(@"viewDidLoad"); 

    [super viewDidLoad]; 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    CGFloat btnHeight = 40; 
    CGFloat btnMargin = 10; 
    CGFloat btnWidth = screenRect.size.width - 2*btnMargin; 
    CGFloat btnULY = screenRect.size.height - btnHeight - 2*btnMargin; 

    btnSelect = [[UIButton alloc] initWithFrame:CGRectMake(btnMargin, btnULY, btnWidth, btnHeight)]; 
    [btnSelect.titleLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE_BUTTON]]; 
    [btnSelect setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
    [btnSelect setTitle :@"Select Image" forState:UIControlStateNormal]; 
    [btnSelect.layer setBorderWidth:4.0f]; 
    [btnSelect.layer setBorderColor:[UIColor blueColor].CGColor]; 
    [btnSelect setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
    [btnSelect setTitleColor:[UIColor grayColor] forState:UIControlStateSelected]; 
    [btnSelect setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; 
    btnSelect.backgroundColor = [UIColor whiteColor]; 
    btnSelect.tag = 4200; 
    [btnSelect addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; 
    btnSelect.userInteractionEnabled = true; 

    CGFloat imgMargin = 60; 
    CGFloat ulx = imgMargin; 
    CGFloat uly = 20; 
    CGFloat imgWidth = screenRect.size.width - 2*imgMargin; 
    CGFloat imgHeight = imgWidth * 9.0/16.0; 

    CGRect imageFrame = CGRectMake(ulx, uly, imgWidth, imgHeight); 

    myImageView1 = [[UIImageView alloc] initWithFrame:imageFrame]; 
    myImageView1.alpha = 1.0f; 
    myImageView1.backgroundColor = [UIColor lightGrayColor]; 
    myImageView1.contentMode = UIViewContentModeScaleAspectFit; 
    myImageView1.opaque = NO; 
    [myImageView1.layer setOpaque:NO]; 
// myImageView.clearsContextBeforeDrawing = YES; 
    [myImageView1.layer setBorderWidth:4.0f]; 
    [myImageView1.layer setBorderColor:[UIColor redColor].CGColor]; 

    myImageView1.image = [UIImage imageNamed:@"imageTest.png"]; 

    uly += imgHeight+8; 
    CGFloat lblHeight = 10; 
    CGRect lblFrame = CGRectMake(ulx, uly, imgWidth, lblHeight); 
    UILabel *myLabel1 = [[UILabel alloc] initWithFrame:lblFrame]; 
    [myLabel1 setFont:[UIFont systemFontOfSize:12]]; 
    myLabel1.text = @"PNG Image from assets"; 

    uly += lblHeight+20; 
    imageFrame.origin.y = uly; 

    myImageView2 = [[UIImageView alloc] initWithFrame:imageFrame]; 
    myImageView2.alpha = 1.0f; 
    myImageView2.backgroundColor = [UIColor lightGrayColor]; 
    myImageView2.contentMode = UIViewContentModeScaleAspectFit; 
    myImageView2.opaque = NO; 
    [myImageView2.layer setOpaque:NO]; 
// myImageView2.clearsContextBeforeDrawing = YES; 
    [myImageView2.layer setBorderWidth:4.0f]; 
    [myImageView2.layer setBorderColor:[UIColor redColor].CGColor]; 

    uly += imgHeight+8; 
    lblFrame.origin.y = uly; 

    UILabel *myLabel2 = [[UILabel alloc] initWithFrame:lblFrame]; 
    [myLabel2 setFont:[UIFont systemFontOfSize:12]]; 
    myLabel2.text = @"UIImagePickerControllerOriginalImage"; 

    uly += lblHeight+20; 
    imageFrame.origin.y = uly; 

    myImageView3 = [[UIImageView alloc] initWithFrame:imageFrame]; 
    myImageView3.alpha = 1.0f; 
    myImageView3.backgroundColor = [UIColor lightGrayColor]; 
    myImageView3.contentMode = UIViewContentModeScaleAspectFit; 
    myImageView3.opaque = NO; 
    [myImageView3.layer setOpaque:NO]; 
// myImageView3.clearsContextBeforeDrawing = YES; 
    [myImageView3.layer setBorderWidth:4.0f]; 
    [myImageView3.layer setBorderColor:[UIColor redColor].CGColor]; 

    uly += imgHeight+8; 
    lblFrame.origin.y = uly; 

    UILabel *myLabel3 = [[UILabel alloc] initWithFrame:lblFrame]; 
    [myLabel3 setFont:[UIFont systemFontOfSize:12]]; 
    myLabel3.text = @"UIImagePickerControllerEditedImage"; 

    // Add elements to this view controller 
    [self.view addSubview:myImageView1]; 
    [self.view addSubview:myLabel1]; 
    [self.view addSubview:myImageView2]; 
    [self.view addSubview:myLabel2]; 
    [self.view addSubview:myImageView3]; 
    [self.view addSubview:myLabel3]; 
    [self.view addSubview:btnSelect]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
#pragma mark - Select Actions 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- (IBAction)onClick: (UIButton *)btn 
{ 
    NSLog(@"onClick"); 
    // DDLogInfo(@"%@:%@", THIS_FILE, THIS_METHOD); 

    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = NO; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; 
    [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; 
    picker.mediaTypes = mediaTypes; 

    [self presentViewController:picker animated:YES completion:NULL]; 
} 


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
#pragma mark - UIImagePicker's Delegate 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    NSLog(@"imagePickerController"); 

    myImage = info[UIImagePickerControllerOriginalImage]; 
    if (myImageView2 != nil) 
     myImageView2.image = myImage; 

    if (myImageView3 != nil) 
     myImageView3.image = info[UIImagePickerControllerEditedImage]; 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 


@end 

这工作,虽然目前还不清楚我为什么:

  1. imagePicker不会为无效的字典键返回nil(我指定了编辑是不允许的)。

  2. '编辑'图像会去除透明度信息。

+0

你的'imageWithImage:'方法有什么意义?为什么不直接在图像视图中使用选定的图像?为什么使用'UIImagePickerControllerEditedImage'而不是'UIImagePickerControllerOriginalImage'? – rmaddy

+0

谢谢,马迪。我imageWithImage:方法_is_真的毫无意义。我在尝试解决问题的选项时实施了它。然而,你的其他建议做到了。使用UIImagePickerControllerOriginalImage作为词典键显示图像应该是。我将添加最终代码和屏幕截图来完成此操作。请提交答案,以便我可以接受。 – gOnZo

回答

0

您的主要问题是在获取所选图像时,使用UIImagePickerControllerEditedImage而不是UIImagePickerControllerOriginalImage。编辑的图像失去了alpha通道,导致黑色而不是透明度。

您也不需要imageWithImage:方法。

+0

谢谢,Maddy。这解决了我的问题。 – gOnZo