Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rescale UImage to New Size

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 522
    Comment on it

    We may be using image sets where few images are pretty big or small in size(say big 500X500 for instance). And if you want to resize the image to your desired size then it’s easiest by creating an extension of UIImage that will draw the image in the required/provided size.

     

    Here are the steps to accomplish:

    1- Create a bitmap-based graphics context

    UIGraphicsBeginImageContext( newSize )

    this will create the current context of bitmap based graphics.

     

    2- Draw the complete image in the rectangle, using required size.

    self.drawInRect(CGRectMake(0,0,newSize.width,newSize.height))

    we are using ‘self’ as we are expecting this to be done by creating a UIImage extension.

     

    3- Now get the image using

    UIGraphicsGetImageFromCurrentImageContext()

    it will return an image based on current context which is bitmap based.

     

    4- Now remove the current bitmap based graphics context, so that it will clear the drawing environment.

    UIGraphicsEndImageContext()



    Here is the code snippet:

    func rescaleImage(toSize newSize:CGSize) -> UIImage {
            
            UIGraphicsBeginImageContext( newSize )
            
            self.drawInRect(CGRectMake(0,0,newSize.width,newSize.height))
            let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: