How to combine two UIImages of difference size without altering their original aspect ratios?

后端 未结 4 1227
说谎
说谎 2021-01-26 10:29

My current method of combining UIImages come from this answer on SO, as well as this popular question on resizing UIImage with aspect ratio. My current issue is the following:<

4条回答
  •  旧巷少年郎
    2021-01-26 11:04

    Mackworth's answer in Swift 3.x

        let fullSize:CGSize = img.size
        let newSize:CGSize = fullSize
        let scale:CGFloat = newSize.height/fullSize.height
        let offset:CGFloat = (newSize.width - fullSize.width*scale)/2
        let offsetRect:CGRect = CGRect.init(x: offset, y: 0, width: newSize.width - offset*2, height: newSize.height)
        print(NSStringFromCGRect(offsetRect))     
    
        UIGraphicsBeginImageContext(newSize);
        self.pictureView.image.draw(in: offsetRect)
        self.annotateView.image.draw(in: CGRect.init(x: 0, y: 0, width: waterMarkImage.size.width, height: waterMarkImage.size.height))
        let combImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        return combImage;
    

提交回复
热议问题