I am building a custom image keyboard and am having trouble properly resizing an image. I have tried a bunch of different methods with no success. The issue is the image is either bigger than I would like it to be or, if I resize it to half of my intended size, then it is more blurry than the original is. In the following example I am attempting to resize the image to the same size (see bottom of post for a screenshot) as it is being displayed on screen. Here is my code for calling the resizing:
println("Starting Size is \(image!.size)")
println("New Size is: \(sender.frame.size))")
println("Initial Scale: \(image!.scale)")
image = imageResize(image!, size: CGSize(width: sender.frame.width, height: sender.frame.height))
println("Final Size is \(image!.size)")
println("Final Scale: \(image!.scale)")
Here is the console output for the above print statements:
Starting Size is (750.0, 750.0)
New Size is: (78.0, 78.0))
Initial Scale: 1.0
Final Size is (78.0, 78.0)
Final Scale: 2.0
Here is the imageResize
function
func imageResize(image:UIImage, size:CGSize)-> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
var context = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
image.drawInRect(CGRect(origin: CGPointZero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
Finally, here is screenshot:
Update:
To clarify, the image on the bottom of the screenshot is from the keyboard. It is a UIButton with the full size (750x750) image placed in it. When I am copying the image to the pasteboard, I begin with the full size image and downsize it to match the frame of the UIButton in the keyboard. So I am trying to get the image in the text message to be the same size and have the same clarity of the image in the keyboard.
Update #2
I updated my image resizing function to adjust the image size depending on the screen scale:
func imageResize(image:UIImage, size:CGSize)-> UIImage {
let scale = UIScreen.mainScreen().scale
let newSize = CGSize(width: size.width / scale, height: size.height / scale)
UIGraphicsBeginImageContextWithOptions(newSize, false, scale)
var context = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
image.drawInRect(CGRect(origin: CGPointZero, size: newSize))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
Here is the console output will some details about the image:
Starting Size is (750.0, 750.0)
New Size is: (78.0, 78.0))
Initial Scale: 1.0
Final Size is (39.0, 39.0)
Final Scale: 2.0
Lastly, here is what the image looks like on the device (iPhone 6):
As you can see, although the image has the proper frame, it appears blurry in comparison to the UIButton in the keyboard. I should be able to achieve the same resolution as the UIButton since I am starting with the full size image in both cases. Any thoughts as to why I am losing resolution?
Update #3
I updated my code to multiply the image by the scale as opposed to dividing by it.
let newSize = CGSize(width: size.width * scale, height: size.height * scale)
Here is the result:
Starting Size is (750.0, 750.0)
New Size is: (78.0, 78.0))
Initial Scale: 1.0
Final Size is (156.0, 156.0)
Final Scale: 2.0
I really appreciate your help, but I am not still not understanding how to pull this all together to get a properly sized photo. To completely clarify my goal, I am trying to make the image copied into the message appear exactly like the image in the UIButton within the keyboard. Here is the code I am using to add the image to the pasteboard to copy it into the message text. The pasteboard code is run after the image is resized:
pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)