Merge two imageViews into one and save iOS swift

萝らか妹 提交于 2019-12-11 04:37:43

问题


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.



来源:https://stackoverflow.com/questions/31670757/merge-two-imageviews-into-one-and-save-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!