Post

Replies

Boosts

Views

Activity

Reply to Imageview - scaleToFill vs. scaleAspectFill
Basically, I have an "original" image view and another image view for cropping an image. The original image view is not touched at all regarding frame, but the cropImageView is.ciImage = CIImage(cgImage: (ZImageCropper.cropImage(ofImageView: cropImageView, withinPoints: [ CGPoint(x: overlay.frame.origin.x, y: overlay.frame.origin.y), //Start point CGPoint(x: overlay.frame.maxX, y: overlay.frame.origin.y), CGPoint(x: overlay.frame.maxX, y: overlay.frame.maxY), CGPoint(x: overlay.frame.origin.x, y: overlay.frame.maxY)])?.cgImage)!)I want to merge the image of the original image view and the cropped image view...This is the code to crop the image:public class ZImageCropper { public class func cropImage(ofImageView:UIImageView, withinPoints points:[CGPoint]) -> UIImage? { //Check if there is start and end points exists if points.count >= 2 { let path = UIBezierPath() let shapeLayer = CAShapeLayer() shapeLayer.fillColor = UIColor.clear.cgColor shapeLayer.lineWidth = 2 var croppedImage:UIImage? for (index,point) in points.enumerated() { //Origin if index == 0 { path.move(to: point) //Endpoint } else if index == points.count-1 { path.addLine(to: point) path.close() shapeLayer.path = path.cgPath ofImageView.layer.addSublayer(shapeLayer) shapeLayer.fillColor = UIColor.black.cgColor ofImageView.layer.mask = shapeLayer UIGraphicsBeginImageContextWithOptions(ofImageView.frame.size, false, 1) if let currentContext = UIGraphicsGetCurrentContext() { ofImageView.layer.render(in: currentContext) } let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() croppedImage = newImage //Move points } else { path.addLine(to: point) } } return croppedImage } else { return nil } } }
Dec ’19
Reply to Imageview - scaleToFill vs. scaleAspectFill
Hi Claude31,thanks for the reply. I think, I got it. hmm.. but then coming to another problem.I want to merge/overlay two images using following code:extension UIImage { static func imageByMergingImages(topImage: UIImage, bottomImage: UIImage, scaleForTop: CGFloat = 1.0) -> UIImage { let size = bottomImage.size let container = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIGraphicsBeginImageContextWithOptions(size, false, 2.0) UIGraphicsGetCurrentContext()!.interpolationQuality = .high bottomImage.draw(in: container) let topWidth = size.width / scaleForTop let topHeight = size.height / scaleForTop let topX = (size.width / 2.0) - (topWidth / 2.0) let topY = (size.height / 2.0) - (topHeight / 2.0) topImage.draw(in: CGRect(x: topX, y: topY, width: topWidth, height: topHeight), blendMode: .normal, alpha: 1.0) return UIGraphicsGetImageFromCurrentImageContext()! } }Whenever, my the contentMode is set to .scaleAspectFill, the merged image doesn't look ok. But if I change it to .scaleToFill, then the merged image looks as expected. Any ideas?
Dec ’19