无需在iphone中以编程方式更改尺寸即可缩小图像尺寸

时间:2011-06-01 05:59:11

标签: iphone

是否有人如何在不改变iphone中以编程方式更改图像尺寸的情况下缩小图像尺寸?

1 个答案:

答案 0 :(得分:0)

//我知道有3种方法 // 1.仅使用调整大小

    UIImageView *iView = ...;
    CGRect orgFrame = iView.frame;
    iView.contentMode = UIViewContentModeScaleAspectFit;
    iView.frame = CGRectMake(CGRectGetMinX(orgFrame), CGRectGetMinY(orgFrame),
                             CGRectGetWidth(orgFrame)*.5, CGRectGetHeight(orgFrame)*.5);

// 2.使用affineTransform进行展示。
//使用3x3矩阵进行缩放 // UIView有

@property(nonatomic) CGAffineTransform transform

//用法:

    UIImageView *iView = ...;
    iView.transform = CGAffineTransform(.5f, .5f);
    // ==> will display 1/2 size view.

// 3.使用CALayer renderInContext:获得缩小的图像

    UIImage *sourceImage = ...;
    CGSize *reducedSize = CGSizeMake(CGRectGetWidth(sourceImage.frame)*.5f, CGRectGetHeight(sourceImage.frame)*.5f);
    UIImage *reducedImage; {
        UIImageView *imageView = [UIImageView initWithImage:sourceImage];

        UIGraphicsBeginImageContext(reducedSize);
        imageView.transform = CGAffineTransform(.5f, .5f);
        [imageView renderInContext:UIGraphicsGetCurrentContext()];

        reduceImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    ...