Saturday 22 November 2014

Jogendra Singh

Image Resize in swift ios8

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 = false
        let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
        UIGraphicsBeginImageContextWithOptions(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+ 

Jogendra Singh

About Jogendra Singh -

I'm Founder of Jogendra.Com. I love to share my bright ideas with the whole blogging community. I'm a good iPhone Application Developer. Apart from Blogging, I love to play Cricket and mobile games (Clash of clans).

Subscribe to this Blog via Email :