How to combine/ merge 2 images into 1

后端 未结 8 1042
粉色の甜心
粉色の甜心 2020-12-07 12:32

I would like to know what to do to save 2 images into 1 image.

One of the photos can be moved, rotated and zoomed in/out...

I\'m doing this, but it basically

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 13:11

    You can create graphics context and draw both images in it. You'll get an image result from both your source images combined.

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

提交回复
热议问题