当一个图像视图与其他图像视图合并时,图像会缩小

时间:2013-12-16 13:28:14

标签: iphone objective-c ipad uiimageview

我想追加来自2个图像视图的两个图像都具有相同的帧

1 。底部图片视图: imageView1

2 。热门图片视图: imageView2

我在 imageView1 上有背景图片,并在 imageView2

上画画

有一次,我需要在 imageView1 imageView2 上附加图片,并仅在 imageView1上显示

我使用以下代码合并/附加两个图像

UIGraphicsBeginImageContext(imageView1.bounds.size);
[imageView1.layer renderInContext:UIGraphicsGetCurrentContext()];
[imageView2.layer renderInContext:UIGraphicsGetCurrentContext()];
lastSavedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imageView1.image = lastSavedImage;

但合并后输出图像每次缩小1像素。

1 个答案:

答案 0 :(得分:0)

我按照弗拉德回答来解决这个问题:

How to combine/ merge 2 images into 1

*

- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
    UIImage *image = nil;
    CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));
    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
    } else {
        UIGraphicsBeginImageContext(newImageSize); 
    }
    [firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2), 
                                        roundf((newImageSize.height-firstImage.size.height)/2))]; 
    [secondImage drawAtPoint:CGPointMake(roundf((newImageSize.width-secondImage.size.width)/2), 
                                         roundf((newImageSize.height-secondImage.size.height)/2))]; 
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

*