UIImage downsizing - loss of permission

I am creating a custom graphical keyboard and I am having problems resizing the image correctly. I tried a bunch of different methods without success. The problem is that the image is larger than I would like, or if I resize it to half my estimated size, then it will be more blurry than the original. In the following example, I am trying to resize an image to the same size (see the bottom of the message for a screenshot) because it is displayed on the screen. Here is my code to call resize:

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 function imageResize

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 a screenshot:

Image resizing example

Update:

, - . UIButton (750x750), . , , UIButton . .

# 2

, :

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
}

:

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

, (iPhone 6):

Second screenshot

, , UIButton . , UIButton, . , ?

# 3

, , .

let newSize = CGSize(width: size.width * scale, height: size.height * scale)

:

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

Update 3

, , , . , , , , UIButton . , , , . :

pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)
+4
2

: . , . , , , . , Retina , .

, , , > 1 .

, , . :

let scale = UIScreen.mainScreen().scale
+1

, , . - . , . :

Pasteboard UIImage

0

All Articles