Simple code for resizing image i.e. for navbar (source link) for png image with native res: 722x1028
let imageView = UIImageView(frame: CGRectMake(0, 0, 0, 60))
imageView.image = UIImage(named: "girl")
imageView.contentMode = UIViewContentMode.ScaleAspectFit
self.navigationItem.titleView = imageView
And I get on iPad2:
I can manually render desired image by changing code:
let imageView = UIImageView(frame: CGRectMake(0, 0, 0, 60))
imageView.image = imageWithImage(UIImage(named: "girl")!, scaledToSize: CGSizeMake(42,60))
self.navigationItem.titleView = imageView
func imageWithImage(image:UIImage, scaledToSize newSize:CGSize) -> UIImage{
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
But this is rather expensive solution.
Is there native library or simple swift solution to make png image automatically resized with good quality?