Merge two imageViews into one and save iOS swift

2019-07-09 02:00发布

问题:

I have 2 imageViews like below. (ScrollView has subview of imageView)

i want to take the image of each one and merged to one.

i tried using taking screenshot and crop. but when it comes to different iphone screensizes and resolutions it doesn't work well.

can any one guide me how to do this?

回答1:

Why not just add them both to one UIView?

It is also possible to add a subview to your image view and extend the frame.

[secondImageView setFrame:CGRectMake(x,y + newImageHeight,width,height)];
[self.myImageView setFrame:CGRectMake(x,y,width,height + newImageHeight)];
[self.myImageView addSubview:secondImageView];


回答2:

I think your way is right, also you can solve scale size problem very easily with this method.

func mergeScreenshot() {
    let layer = UIApplication.sharedApplication().keyWindow.layer
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); // reconsider size property for your screenshot

    layer.renderInContext(UIGraphicsGetCurrentContext())
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
}

But you should edit sizes again. For example if you're using autolayout you can create IBOutlet and than use it instead of layer.frame.size property which I used.

Hope it helps.