开发者

Reduce image size without changing the dimensions programmatically in iphone

Does any one how to reduce image size w开发者_开发问答ithout changing the dimensions of that image programmaticallyin iphone?


// There is 3 way that were known to me.
// 1. Use just resize

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

// 2. Use affineTransform for just display.
// It use 3x3 matrix for scaling.
// UIView has

@property(nonatomic) CGAffineTransform transform

// Usage:

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

// 3. Use CALayer renderInContext: for get reduced Image

    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();
    }
    ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜