如何降低 iPhone Objective-c 中的图像质量/尺寸?

发布于 2024-08-18 07:01:44 字数 516 浏览 2 评论 0原文

我有一个应用程序,可以让用户用他/她的 iPhone 拍照并将其用作应用程序的背景图像。我使用 UIImagePickerController 让用户拍照并将背景 UIImageView 图像设置为返回的 UIImage 对象。

IBOutlet UIImageView *backgroundView;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
 backgroundView.image = image;
 [self dismissModalViewControllerAnimated:YES];
}

这一切都很好。 如何将 UIImage 的大小减小到 480x320,以便我的应用程序可以提高内存效率? 我不在乎是否会失去任何图像质量。

提前致谢。

I have an app that lets the user take a picture with his/her iPhone and use it as a background image for the app. I use UIImagePickerController to let the user take a picture and set the background UIImageView image to the returned UIImage object.

IBOutlet UIImageView *backgroundView;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
 backgroundView.image = image;
 [self dismissModalViewControllerAnimated:YES];
}

This all works fine.
How can I reduce the size of the UIImage to 480x320 so my app can be memory efficient?
I don't care if I loose any image quality.

Thanks in advance.

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

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

发布评论

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

评论(3

窗影残 2024-08-25 07:01:44

您可以创建图形上下文,以所需的比例将图像绘制到其中,然后使用返回的图像。例如:

UIGraphicsBeginImageContext(CGSizeMake(480,320));

CGContextRef            context = UIGraphicsGetCurrentContext();

[image drawInRect: CGRectMake(0, 0, 480, 320)];

UIImage        *smallImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();    

You can create a graphics context, draw the image into that at the desired scale, and use the returned image. For example:

UIGraphicsBeginImageContext(CGSizeMake(480,320));

CGContextRef            context = UIGraphicsGetCurrentContext();

[image drawInRect: CGRectMake(0, 0, 480, 320)];

UIImage        *smallImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();    
玩世 2024-08-25 07:01:44

我知道这个问题已经解决了,但是如果有人(像我一样)想要缩放图像并保持纵横比,则以下代码可能会有所帮助:

-(UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)size
{
    float width = size.width;
    float height = size.height;

    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, width, height);

    float widthRatio = image.size.width / width;
    float heightRatio = image.size.height / height; 
    float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;

    width = image.size.width / divisor; 
    height = image.size.height / divisor;

    rect.size.width  = width;
    rect.size.height = height;

    if(height < width)
        rect.origin.y = height / 3;

    [image drawInRect: rect];

    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();   

    return smallImage;
}

I know this question is already solved, but just if someone (like i did) wants to scale the image keeping the aspect ratio, this code might be helpful:

-(UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)size
{
    float width = size.width;
    float height = size.height;

    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, width, height);

    float widthRatio = image.size.width / width;
    float heightRatio = image.size.height / height; 
    float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;

    width = image.size.width / divisor; 
    height = image.size.height / divisor;

    rect.size.width  = width;
    rect.size.height = height;

    if(height < width)
        rect.origin.y = height / 3;

    [image drawInRect: rect];

    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();   

    return smallImage;
}
时常饿 2024-08-25 07:01:44

使用 contentOfFile 并确保所有图像都是 .png。 Apple 针对 png 进行了优化。

哦,使用 contentOfFile 而不是 imageName 方法。有几个原因。即使在调用 [release] 后,由 ImageName 引入内存的图像仍保留在内存中。

别问我为什么。苹果是这么告诉我的。

罗伊德尔·克拉克

Use contentOfFile and make sure that all of your images are .png. Apple is optimized for png.

Oh, use the contentOfFile not the imageName method. Several reasons for that. Images that is brought in memory by ImageName remained in memory even after a [release] is called.

Dont ask my why. The apple told me so.

Roydell Clarke

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