Luca Alberto
Luca Alberto

Reputation: 1255

How to avoid low quality when merging images?

I am using this function:

var size = CGSize(width: 320, height: 358) // 348
    UIGraphicsBeginImageContext(size)
    //let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    let areaSize = CGRect(x: 20, y: 5, width: size.width-40, height: size.height-30)
    let areaSizeTop = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    bottomImage.drawInRect(areaSize)
    topImage.drawInRect(areaSizeTop, blendMode: kCGBlendModeNormal, alpha: 1) //0.8 default
    var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage

to merge two different images (A) + (B), but when i do that the final image (C) result is of very low quality, how can I fix that? There is some code to write to make the (C) image looking better? Here is a screen of the (C,merged) image: image

Upvotes: 0

Views: 110

Answers (1)

Duncan C
Duncan C

Reputation: 131418

You should be using UIGraphicsBeginImageContextWithOptions, and passing in a scale of 0. Using a scale of 0 causes it to match the scale of the current device's screen. (Retina for retina devices)

With the call UIGraphicsBeginImageContext, you are forcing the output image to be non-retina. On a retina device (which is all current devices except the iPad 2) you lose half of your resolution. That's probably the cause of your problem.

As @Daij-Djan points out in his comment, you are creating a pretty small image. Once you fix the begin image context call, you might want to set a larger image context if you have a larger output image.

Upvotes: 4

Related Questions