如何减小 UIImageView 上图像显示的尺寸

发布于 2024-11-01 23:16:37 字数 223 浏览 1 评论 0原文

我的问题是,在我的应用程序中,我有 UIImageviewbutton1button2button1 用于访问保存的照片库中的图像,button2 用于将图像存储在数据库中,

但图像尺寸为(800x800),非常大,我想将其存储为(50x50)尺寸

如何单击button2时缩小图像尺寸?

My problem is that, in my app I have UIImageview, button1, button2. button1 is used to access images from saved photolibrary and button2 is used to store image in database,

But image size is (800x800) which is very large, I want to store it at (50x50) size

How to reduce size of image when button2 is clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城之時有危險 2024-11-08 23:16:37
// grab the original image
UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];

UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:50/800 orientation:UIImageOrientationUp];
// grab the original image
UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];

UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:50/800 orientation:UIImageOrientationUp];
遥远的她 2024-11-08 23:16:37

这个类别将为您完成这项工作 -

UIImage+Additions.h -

@interface UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size;
@end

UIImage+Additions.m -

@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

This category will do the job for you -

UIImage+Additions.h -

@interface UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size;
@end

UIImage+Additions.m -

@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
天涯离梦残月幽梦 2024-11-08 23:16:37
+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

请参阅上一个问题使用 iPhone SDK 调整/优化图像尺寸的最简单方法是什么?以供进一步参考

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

Pls see this previous SO question What's the easiest way to resize/optimize an image size with the iPhone SDK? for further reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文