具有预定义大小的ios打印

时间:2012-06-13 12:38:23

标签: ios printing

我制作了一个必须打印图像的应用程序。我需要打印预定义大小的图像。

例如我有一些50 x 50像素的图像我想将其调整为一些新的尺寸像素,打印后我会得到尺寸为5 x 5厘米的图像。

请参阅附件:

enter image description here

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

嘿,我使用下面的代码来调整我的应用中的图像

根据需要设置尺寸

  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];