At present i’m utilizing the next swift code for scaling cropping and making a round picture out of the handed UIImage . The whole lot is working till iOS 18 however after that i can observe crashes . I discovered that from iOS 18 onwards the next three strategies have been deprecated;
UIGraphics.BeginImageContextWithOptions(dimension, false, ScreenDensity);
var picture = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
From different SO posts it’s endorsed to make use of the you’ll use UIGraphicsImageRenderer
and its related capabilities. Right here is the complete code utilizing the above deprecated APIs.
func scaleCropAndCircleImage(picture:UIImage, body:CGRect) throws -> UIImage {
enum FunctionError: Error {
case nilContext
case nilImage
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(body.dimension.width, body.dimension.peak), false, 0.0)
guard let context = UIGraphicsGetCurrentContext() else { throw FunctionError.nilContext }
// width and heights of handed values of uiimage and CGRect
let passedimageWidth = picture.dimension.width
let passedimageHeight = picture.dimension.peak
let passedrectWidth = body.dimension.width
let passedrectHeight = body.dimension.peak
// scale issue calculation
let scaleFactorX = passedrectWidth/passedimageWidth
let scaleFactorY = passedrectHeight/passedimageHeight
// centre of the circle calculation
let imageCentreX = passedrectWidth/2
let imageCentreY = passedrectHeight/2
// Creating and clipping to a CIRCULAR Path
let radius = passedrectWidth/2
context.beginPath()
context.addArc(middle: CGPoint(x: imageCentreX, y: imageCentreY), radius: radius, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
context.closePath()
context.clip()
//Seting the scaling issue for graphics context
context.scaleBy(x: scaleFactorX, y: scaleFactorY)
let myRect:CGRect = CGRectMake(0, 0, passedimageWidth, passedimageHeight)
picture.draw(in: myRect)
let newCroppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let newCroppedImage else { throw FunctionError.nilImage }
return newCroppedImage
}
Now i’m making an attempt to exchange the deprecated capabilities through the use of the UIGraphicsImageRenderer
however i’m caught within the mid method the best way to convert the already used logic for the UIGraphicsImageRenderer
capabilities . Under is the code i’ve tried to this point. Can somebody assist me on this in order that the under perform can settle for the UIImage and crop it right into a circle as completed in current code?
import UIKit
extension UIImage {
func roundedCornerImage(with radius: CGFloat) -> UIImage {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
let renderer = UIGraphicsImageRenderer(dimension: dimension, format: format)
return renderer.picture { rendererContext in
let rect = CGRect(origin: .zero, dimension: dimension)
let path = UIBezierPath(roundedRect: rect,
byRoundingCorners: .allCorners,
cornerRadii: CGSize(width: radius, peak: radius))
path.shut()
let cgContext = rendererContext.cgContext
cgContext.saveGState()
path.addClip()
draw(in: rect)
cgContext.restoreGState()
}
}
}