21

您好我正在开发需要合并两个 Image 的应用程序,通过合并两个 Image 我的图像大小为 320*240 我希望大小为 320 * 480 。我怎样才能以编程方式做到这一点。以下是图片

第一张图片 第二张图片

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

在此处输入图像描述

4

10 回答 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];
于 2013-08-16T12:52:54.697 回答
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];
于 2015-02-17T15:49:37.393 回答
8

斯威夫特版本

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!)
于 2015-10-01T07:48:05.367 回答
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()
}
于 2017-01-17T03:46:11.887 回答
2

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

于 2013-08-16T12:42:05.460 回答
1

斯威夫特 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
}
于 2016-12-14T16:32:49.390 回答
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)
于 2017-12-07T20:02:14.597 回答
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();
}
于 2015-04-06T08:29:45.833 回答
0

我认为您必须获取 ypur 图像的上下文,然后对其进行标记。

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

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

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

于 2013-08-16T12:39:35.200 回答
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();
于 2013-08-16T12:38:32.357 回答