组合UIImageViews会产生0字节的空图像

时间:2014-01-21 04:12:59

标签: ios iphone uiimageview uiimage

我正在尝试将两个UIImageView合并为combinedView。我遇到的问题是,在我将self.firstImageViewself.secondImageView作为子视图添加到combinedView后,我检查combinedView.image的字节大小时得到的输出是0.我需要combinedView.image最终在JEPGRepresentation中传递给我的后端,所以我不确定如何将这些视图组合在一起,或者我现在做错了什么。

感谢您的帮助!

UIImageView *combinedView = [[UIImageView alloc] init];
[combinedView addSubview:self.firstImageView];
[combinedView sendSubviewToBack:self.firstImageView];
[combinedView addSubview:self.secondImageView];
[combinedView bringSubviewToFront:self.secondImageView];

NSData *firstPicData = UIImageJPEGRepresentation(self.firstImageView.image, 1.0);
NSLog(@"Size of first:%d",[firstPicData length]); //outputs a non-zero number

NSData *secondPicData = UIImageJPEGRepresentation(self.secondImageView.image, 1.0);
NSLog(@"Size of second:%d",[secondPicData length]); //also outputs a non-zero number

NSData *combinedPicData = UIImageJPEGRepresentation(combinedView.image, 1.0);
NSLog(@"Size of combined:%d",[combinedPicData length]); //! outputs 0 bytes

1 个答案:

答案 0 :(得分:0)

您正在添加self.firstImageView和self.secondImageView作为组合视图的子视图,而且您没有将任何图像分配给combinedView。所以[combinedPicData length]总是只有0。

请参阅以下链接以合并两张图片。

iOS - Merging two images of different size

blend two uiimages based on alpha/transparency of top image

Merge Two Image on to one Image programmatically in iphone

How to merge two half images in ios app?