user2003416
user2003416

Reputation: 159

Resizing image without losing its quality

When i send import images[other images] to server taking less time than iphone images[taking picture through default camera].Is there option to resize an image without losing its quality?

Upvotes: 2

Views: 4423

Answers (2)

Paras Joshi
Paras Joshi

Reputation: 20541

try this bellow method ...

Call like bellow...

 UIImage *imgBig = [self imageWithImage:yourImage scaledToSize::yourNewImageSize];

and use this bellow method..

-(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{

    UIGraphicsBeginImageContextWithOptions(newSize, YES, image.scale);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

Upvotes: 2

amar
amar

Reputation: 4345

Use this code it will help

+ (UIImage *)scaleImage:(UIImage *)image maxWidth:(int) maxWidth maxHeight:(int) maxHeight
{
    CGImageRef imgRef = image.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    if (width <= maxWidth && height <= maxHeight)
    {
        return image;
    }

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);

    if (width > maxWidth || height > maxHeight)
    {
        CGFloat ratio = width/height;

        if (ratio > 1)
        {
            bounds.size.width = maxWidth;
            bounds.size.height = bounds.size.width / ratio;
        }
        else
        {
            bounds.size.height = maxHeight;
            bounds.size.width = bounds.size.height * ratio;
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, scaleRatio, -scaleRatio);
    CGContextTranslateCTM(context, 0, -height);
    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);

    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;

}

Upvotes: 3

Related Questions