How to resize an image?
In this tutorials We are going to learn "How can resize UIImage in swift language". Without resize image we can get full image in our view
First I am showing image that in original size
If we check this image size than we see image size is "1366 × 768" .
We're not able to show full image in our iPhone screen.
Now Need to check How looks this iPhone without resize
Setting image in backgroud of UIView
self.view.backgroundColor = UIColor(patternImage:UIImage(named: "bg Image.jpg")!)
After doing this setup, we see in simulator this type .
In this image we can see image's only few part is showing., so we need to resize image
Resizing UIImage in swift
I have made function that can we use for resize image, In that we need to pass original image and size that we need .
func imageResize (imageObj:UIImage, sizeChange:CGSize)-> UIImage{let hasAlpha = falselet scale: CGFloat = 0.0 // Automatically use scale factor of main screenUIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))let scaledImage = UIGraphicsGetImageFromCurrentImageContext()return scaledImage}
How to call this function : -
var mainScreenSize : CGSize = UIScreen.mainScreen().bounds.size // Getting main screen size of iPhone
var imageObbj:UIImage! = self.imageResize(UIImage(named: "bg Image.jpg")!, sizeChange: CGSizeMake(mainScreenSize.width, mainScreenSize.height))
self.view.backgroundColor = UIColor(patternImage:imageObbj)
After doing this setup we see again image in device that looks like this :-
We can see in this image is fit in screen .
Thanks for Visit , Also can visit on My blog Facebook page iPhone & iPad Application Development Help World and also can visit Google+