QueueOverFlow
QueueOverFlow

Reputation: 1336

Resizing an Image Objective C

I am zooming an image in scrollView, I wants the image size resize according to zoom, I know i have to write some code in scrollViewDidEndZooming method.

I am following this link but I think this is not updated ( I got error when implementing the resize Image category), So please direct me in right way, How to resize the image according to zoom scale.

Thanks

Upvotes: 0

Views: 302

Answers (1)

user2155906
user2155906

Reputation: 149

#define DOC_WIDTH 505
#define DOC_HEIGHT 802


+(UIImage*)imageResizeWithBorderFromImage:(UIImage*)source
{

//******* Change size
    int tnw;
    int tnh;

    double x_ratio = DOC_WIDTH / source.size.width;
    double y_ratio = DOC_HEIGHT / source.size.height;
    if (source.size.width <= DOC_WIDTH && source.size.height <= DOC_HEIGHT){
        tnw=(int)source.size.width ;
        tnh=(int)source.size.height;
    }
    else if ((x_ratio * source.size.height) < DOC_HEIGHT) {
        tnh = (int)(ceil(x_ratio * source.size.height));
        tnw = (int)DOC_WIDTH ;
    }
    else {
        tnw = (int)(ceil(y_ratio * source.size.width));
        tnh = (int)DOC_HEIGHT;
    }
    CGSize size = CGSizeMake(tnw, tnh);

    //CGSize size = [source size];
    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context,(0/255.f),(162/255.f),(237/255.f), 1.0);
    CGContextStrokeRect(context, rect);
    UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return testImg;
}

Upvotes: 1

Related Questions