I am working on SwiftUI
app where I have to draw a transparent image over another image, then overlay it with a text.
currently i am achieving this with UIGraphicsBeginImageContextWithOptions
but it's taking more time as i have an array of nearly 1700 images . Is there any other alternative we can use instead of UIGraphicsBeginImageContextWithOptions
?
for mdl in bottomImage{
for img in Constants.episodesFrames {
autoreleasepool {
imageName += 1
let text = mdl!.text
let imageSelected = mdl?.image.scaleImageToSize(newSize: CGSize(width: 660, height: 700))
let topNew = UIImage(named: img)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
imageSelected!.draw(in: CGRect(origin: .zero, size: newSize))
topNew!.draw(in: CGRect(origin: .zero, size: newSize))
let font=UIFont(name: "JosefinSans-SemiBold", size: 35)!
let text_style=NSMutableParagraphStyle()
text_style.lineBreakMode = .byWordWrapping
text_style.alignment=NSTextAlignment.center
let text_color=UIColor(named: "videoText")
let attributes=[NSAttributedString.Key.font:font, NSAttributedString.Key.paragraphStyle:text_style, NSAttributedString.Key.foregroundColor:text_color]
let text_y=((newSize.height)/1.19)
let text_rect=CGRect(x: 0, y: text_y, width: newSize.width-30, height: newSize.height)
text.draw(in: text_rect, withAttributes: attributes as [NSAttributedString.Key : Any])
//Conditions for changing the topImage
if imageName >= 391 && imageName <= 440{
let frameCenter = UIImage(named:Constants.repeatFrames[imageName-391])
frameCenter?.draw(in: CGRect(origin: .zero, size: newSize))
}else if imageName >= 638 && imageName <= 687{
let frameCenter = UIImage(named:Constants.repeatFrames[imageName-638])
frameCenter?.draw(in: CGRect(origin: .zero, size: newSize))
}else if imageName >= 885 && imageName <= 934{
let frameCenter = UIImage(named:Constants.repeatFrames[imageName-885])
frameCenter?.draw(in: CGRect(origin: .zero, size: newSize))
}else if imageName >= 1132 && imageName <= 1181{
let frameCenter = UIImage(named:Constants.repeatFrames[imageName-1132])
frameCenters?.draw(in: CGRect(origin: .zero, size: newSize))
}else if imageName >= 1379 && imageName <= 1428{
let frameCenter = UIImage(named:Constants.repeatFrames[imageName-1379])
frameCenter?.draw(in: CGRect(origin: .zero, size: newSize))
}
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
saveImageTempDirectory(image: newImage!, imageName: "Image\(imageName).jpg")
}
}
}