首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何缩小UIImageView上的图像显示尺寸

问如何缩小UIImageView上的图像显示尺寸
EN

Stack Overflow用户
提问于 2011-04-14 17:36:41
回答 3查看 3.4K关注 0票数 1

我的问题是,在我的应用程序中有UIImageview,button1,button2。用button2将图像存入数据库,用button1从保存的图片库中存取图像,

但是图像大小是(800x800),这是非常大的,我想以(50x50)大小存储它

如何在点击button2时缩小图片大小?

EN

回答 3

Stack Overflow用户

发布于 2011-04-14 17:44:55

代码语言:javascript
复制
// grab the original image
UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];

UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:50/800 orientation:UIImageOrientationUp];
票数 2
EN

Stack Overflow用户

发布于 2011-04-14 17:44:56

这个类别将为您做这项工作-

UIImage+Additions.h -

代码语言:javascript
复制
@interface UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size;
@end

UIImage+Additions.m -

代码语言:javascript
复制
@implementation UIImage (UIImageAdditions)

- (UIImage*)scaleToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;
}
@end
票数 2
EN

Stack Overflow用户

发布于 2011-04-14 17:44:48

代码语言:javascript
复制
+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

请参阅前面的SO问题What's the easiest way to resize/optimize an image size with the iPhone SDK?以获得进一步的参考

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5661199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档