Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27113

ios printing with a predefined size

I make an app that have to printing image. I need to print image with a predefined size.

For example I have some image 50 x 50 px I want to resize it to some new size pixel that after printing I get image with size 5 x 5 cm.

Please see an attachment:

enter image description here

Thanks for help!

Upvotes: 0

Views: 159

Answers (1)

Hiren
Hiren

Reputation: 12780

Hey i have used below code for resize the image in my app

Set the size according you want

  UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:@"pic"]]]];
        // Resize, crop the image to make sure it is square and renders
        // well on Retina display
        float ratio;
        float delta;
        float px = 100; // Double the pixels of the UIImageView (to render on Retina)
        CGPoint offset;
        CGSize size = image.size;
        if (size.width > size.height) {
            ratio = px / size.width;
            delta = (ratio*size.width - ratio*size.height);
            offset = CGPointMake(delta/2, 0);
        } else {
            ratio = px / size.height;
            delta = (ratio*size.height - ratio*size.width);
            offset = CGPointMake(0, delta/2);
        }
        CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                     (ratio * size.width) + delta,
                                     (ratio * size.height) + delta);
        UIGraphicsBeginImageContext(CGSizeMake(px, px));
        UIRectClip(clipRect);
        [image drawInRect:clipRect];
        UIImage *imgThumb = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        [img setImage:imgThumb];

Upvotes: 1

Related Questions