Merge Two Image on to one Image programmatically in iphone

前端 未结 10 784
旧巷少年郎
旧巷少年郎 2020-12-04 17:01

Hello I am developing the application which requires to merge two Image , My image size are 320*240 by merging both Image I want the size to be 320 * 480 . How can i do this

相关标签:
10条回答
  • 2020-12-04 17:46

    //ADDED Swift 3.2 and 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)
    
    0 讨论(0)
  • 2020-12-04 17:47

    you can use two different image-views one below other.and assign two different images.

    0 讨论(0)
  • 2020-12-04 17:48

    Swift Version

    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
    }
    

    You will get your image to call this function like this :

    var myimage1 = UIImage(named: "image1.png")
    var myimage2 = UIImage(named: "image2.png")
    
    var finalMixedImage = getMixedImg(myimage1!, image2: myimage2!)
    
    0 讨论(0)
  • 2020-12-04 17:50
     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 讨论(0)
提交回复
热议问题