在 iPhone 上以编程方式将两个图像合并成一个图像

21

你好,我正在开发一个需要合并两张图片的应用程序,我的图片大小是320*240,通过合并这两张图片,我希望尺寸变为320*480。请问如何在程序中实现这个功能。以下是这两张图片:

第一张图片 第二张图片

===================================================================================

输入图像描述


1
你想要合并它们还是将它们放在彼此之上? - IronManGill
你是从两个 UIImage 开始的吗? - Marcus Adams
我想将它们彼此叠放。因为我展示了两张图片,所以我不想让它们重叠,我想覆盖488高度。 - krish
查看我的答案,其中包含了Swift 3的实现。 - nurnachman
10个回答

35

刚刚测试过,根据您处理的图像大小创建上下文,并将它们叠加在一起(假设它们具有相同的宽度):

UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];

CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);

UIGraphicsBeginImageContext(size);

[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//Add image to view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[self.view addSubview:imageView];

1
奇怪的是,这似乎会显著降低我的质量。有什么想法为什么会这样? - Dinkman123
1
关于图像质量的问题,请参见我下面的回答。 - Johnny Z

15

为了避免影响图像质量,请使用 UIGraphicsBeginImageContextWithOptions

如果将值设置为0.0,则比例因子将设置为设备主屏幕的比例因子。

UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];

CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);

UIGraphicsBeginImageContextWithOptions(size, false, 0.0) // Use this call

[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//Add image to view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[self.view addSubview:imageView];

7

Swift 版本

func getMixedImg(image1: UIImage, image2: UIImage) -> UIImage {

    var size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height)

    UIGraphicsBeginImageContext(size)

    image1.drawInRect(CGRectMake(0,0,size.width, image1.size.height))
    image2.drawInRect(CGRectMake(0,image1.size.height,size.width, image2.size.height))
    var finalImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return finalImage
}

您可以使用以下方式调用此函数来获取您的图像:
var myimage1 = UIImage(named: "image1.png")
var myimage2 = UIImage(named: "image2.png")

var finalMixedImage = getMixedImg(myimage1!, image2: myimage2!)

优雅的代码 - 我用 Swift 3 实现了它 - 请看下面 - 并编写了一个接受数组而不是1,2个图像的函数... - nurnachman

3

这个实现支持可变参数,因此您可以垂直合并无限数量的图像:

public static func mergeVertically(images: UIImage...) -> UIImage? {
    let maxWidth = images.reduce(0.0) { max($0, $1.size.width) }
    let totalHeight = images.reduce(0.0) { $0 + $1.size.height }

    UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: totalHeight), false, 0.0)
    defer {
        UIGraphicsEndImageContext()
    }

    let _ = images.reduce(CGFloat(0.0)) {
        $1.draw(in: CGRect(origin: CGPoint(x: 0.0, y: $0), size: $1.size))
        return $0 + $1.size.height
    }

    return UIGraphicsGetImageFromCurrentImageContext()
}

2

您可以使用两个不同的图像视图,一个在另一个下方,并分配两个不同的图像。


1

//新增了 Swift 3.2 和 4.0

@IBOutlet weak var imgCamera1: UIImageView!
@IBOutlet weak var imgCamera2: UIImageView!

    let image1 = imgCamera1.image
    let image2 = imgCamera2.image

    let size = CGSize(width: image1?.size.width ?? 0.0, height: (image1?.size.height)! + (image2?.size.height)!)
    UIGraphicsBeginImageContext(size)
    image1?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: image1?.size.height ?? 0.0))
    image2?.draw(in: CGRect(x: 0, y: image1?.size.height ?? 0.0, width: size.width, height: image2?.size.height ?? 0.0))

    let finalImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    //Add image to view
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: finalImage?.size.width ?? 0.0, height: finalImage?.size.height ?? 0.0))
    imageView.image = finalImage

    //Displaying Image
   // view.addSubview(imageView)

1

Swift 3:

接受一个图片数组并返回将这些图片叠加在一起的图片,考虑到最大尺寸,保证不失真。

func combineTopToBottom(imagesArray:[UIImage]) -> UIImage? {
    var dimensions = CGSize(width: 0.0, height: 0.0)
    for image in imagesArray {
        dimensions.width = max(dimensions.width, image!.size.width)
        dimensions.height += max(dimensions.height, image!.size.height)
    }

    UIGraphicsBeginImageContext(dimensions)

    var lastY = CGFloat(0.0)
    for image in imagesArray {
        image?.draw(in:CGRect(x: 0, y: lastY, width: dimensions.width, height: image!.size.height))
        lastY += image!.size.height
    }

    let finalImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return finalImage
}

0
#pragma mark - merging two images
-(void)mergeimage :(UIImage*)firstImage withImage:(UIImage*)secondImage{



    CGSize size = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));


    UIGraphicsBeginImageContext(size);


    [firstImage drawInRect:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,size.width/2,size.height)];


    [secondImage drawInRect:CGRectMake(self.view.frame.origin.x+(size.width/2),self.view.frame.origin.y,size.width/2,size.height)];

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();


    UIGraphicsEndImageContext();
}

请问您能否提供更多关于您的解决方案的细节? - abarisone
两张图片垂直合并,任何宽高的图片都可以合并。 - yashwant patil

0
 UIImage *Image1 = [[UIImage alloc] initWithData:data]; 
 UIImage *Image2 = [[UIImage alloc] initWithData:data]; 
 // Set up width height with values.
        CGSize newSize = CGSizeMake(width, height);
        UIGraphicsBeginImageContext( newSize );

        [Image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

        [Image2 drawInRect:CGRectMake(newSize.width,newSize.height,newSize.width,newSize.height*2) blendMode:kCGBlendModeNormal alpha:1.0];

        UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

0

我认为你必须获取你的图片上下文,然后合并它。

UIImage *eyes = [UIImage imageNamed:@"eyes"];
    UIGraphicsBeginImageContext(CGSizeMake(m, n));
    CGContextRef context1 = UIGraphicsGetCurrentContext(); 

    UIImage *mouth = [UIImage imageNamed:@"mouth"];
    UIGraphicsBeginImageContext(CGSizeMake(m, n));
    CGContextRef context2 = UIGraphicsGetCurrentContext(); 

通过绘制不同边界的上下文进行合并


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接