将png加载到UIImageView iOS中

时间:2012-07-18 19:17:38

标签: objective-c ios cocoa-touch uiimageview png

我有两个图像需要相互重叠,它们都是png图像(因为我需要能够使它们透明)。但是,当我将它们加载到我的xib文件中的UIImage视图中时,它们都没有显示!当我尝试使用相同图像的jpg格式时,它工作正常,但由于jpg不支持透明度,我需要的叠加效果会丢失。如何让png图像实际显示在窗口中?

Interface Builder

2 个答案:

答案 0 :(得分:4)

这是一种比代码“Crappy”Builder更容易完成的任务:

CGRect imageFrame = CGRectMake(x, y, width, height);

UIImage *image1 = // however you obtain your 1st image
UIImage *image2 = // however you obtain your 2nd image

UIImageView *imgView1 = [[UIImageView alloc] initWithImage:image1];
// Adjust the alpha of the view
imgView1.alpha = 1.0f; // This is most advisably 1.0 (always)
imgView1.backgroundColor = [UIColor clearColor];
imgView1.frame = imageFrame;

UIImageView *imgView2 = [[UIImageView alloc] initWithImage:image2];
// Adjust the alpha of the view
imgView1.alpha = 0.5f; // or whatever you find practical
imgView1.backgroundColor = [UIColor clearColor];
imgView2.frame = imageFrame;

// Assume a view controller
[self.view addSubview:imgView1];
[self.view addSUbview:imgView2]; // add the image view later which you wanna be on the top of the other one

// If non-ARC environment, we need to take care of the percious RAM
[imgView1 release];
[imgView2 release];

答案 1 :(得分:1)

尝试在照片编辑器(如photoshop或pixelmator)中打开png图像,然后将其另存为非隔行扫描(在png的保存选项中)。