How to Resize image in Swift in iOS?

Image comes form the server, The image have different size because of uploaded on server from different device, So that image have to resize before the use of images  in apps, Here to resize image programmatically in swift,

Swift : Resize image in swift :

func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / image.size.width
    let heightRatio = targetSize.height / image.size.height

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
    } else {
        newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRectMake(0, 0, newSize.width, newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.drawInRect(rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}
Use of the above function, You can resize your image. Here use of above function code resizing image dimension 200*200. call the above function.
self.ResizeImage(UIImage(named: "yourImageName")!, targetSize: CGSizeMake(200.0, 200.0))

Objective C : Resize image in Objective C  :
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Use the above function in objective-c as below :
[Self imageWithImage:"YourimageName" scaledToSize:CGSizeMake(200.0, 200.0)];
Step of Multiple storyboard using storyboard references. Sample demo with steps. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpufasdasd
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
Xcode 7 added new features for storyboards that is a single storyboard into multiple storyboards and link them visually via storyboard references. - See more at: http://iosdevcenters.blogspot.in/2015/11/how-to-use-multiple-storyboard-using.html#sthash.ShI2bHHR.dpuf
How to Resize image in Swift in iOS? How to Resize image in Swift in iOS? Reviewed by KIRIT MODI on 21:33:00 Rating: 5

No comments:

Powered by Blogger.