Nick
Nick

Reputation: 77

iOS Resize and save as resized image

Code works fine but when i save this image , it saves as original size. how can i save this as scaled size ?

- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {

CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if( image.size.width > image.size.height ) {
    scaleFactor = image.size.width / image.size.height;
    scaledSize.width = newSize.width;
    scaledSize.height = newSize.height / scaleFactor;
}
else {
    scaleFactor = image.size.height / image.size.width;
    scaledSize.height = newSize.height;
    scaledSize.width = newSize.width / scaleFactor;
}

UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

   return scaledImage;
}

Upvotes: 1

Views: 958

Answers (2)

Nikolay Nankov
Nikolay Nankov

Reputation: 273

Set the scale factor in UIGraphicsBeginImageContextWithOptions to 1.0. Setting it to 0.0 sets the scale to the device's screen scale factor.

Upvotes: 2

Bilal
Bilal

Reputation: 588

In interface builder there is a property of UIImageView named "mode". Try setting it from there.

Upvotes: 0

Related Questions