【问题标题】:Merge Two Image on to one Image programmatically in iphone在iphone中以编程方式将两个图像合并到一个图像上
【发布时间】:2017-12-08 04:02:14
【问题描述】:

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

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

【问题讨论】:

  • 你想合并它们还是将它们放在一起?
  • 你是从两个UIImages 开始的吗?
  • 我想把它们放在一起。当我显示两个图像时,我不想重叠图像,我想覆盖 488 高度
  • 查看我对 Swift 3 实现的回答

标签: iphone ios


【解决方案1】:

刚刚对此进行了测试,根据您正在使用的图像的大小创建上下文并将它们相互叠加(假设它们具有相同的宽度):

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];

【讨论】:

  • 奇怪的是,这似乎大大降低了我的质量。任何想法为什么会这样?
  • 在下面查看我关于图像质量的回答
【解决方案2】:

为避免降低图像质量,请使用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];

【讨论】:

    【解决方案3】:

    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 图像的函数......
    【解决方案4】:

    此实现适用于可变参数,因此您可以传递无限数量的图像以垂直合并:

    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()
    }
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        斯威夫特 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
        }
        

        【讨论】:

          【解决方案7】:

          //添加了 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)
          

          【讨论】:

            【解决方案8】:
             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();
            

            【讨论】:

              【解决方案9】:

              我认为您必须获取 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(); 
              

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

              【讨论】:

                【解决方案10】:
                #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();
                }
                

                【讨论】:

                • 能否请您添加有关您的解决方案的更多详细信息?
                • 两张图片垂直合并,任意宽高的图片都可以合并。
                猜你喜欢
                • 2016-04-15
                • 1970-01-01
                • 1970-01-01
                • 2017-11-03
                • 1970-01-01
                • 1970-01-01
                • 2014-06-14
                • 2012-09-16
                • 2020-06-11
                相关资源
                最近更新 更多