Merge two imageViews into one and save iOS swift

2019-07-09 02:13发布

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

enter image description here

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?

2条回答
乱世女痞
2楼-- · 2019-07-09 02:40

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.

查看更多
Explosion°爆炸
3楼-- · 2019-07-09 02:45

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];
查看更多
登录 后发表回答