更改图像尺寸后,图像的透明部分变为黑色 [英] After changing image size, image's transparent part became black

查看:179
本文介绍了更改图像尺寸后,图像的透明部分变为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我的视图更改图像大小,所以我为此编写了此代码。

I am trying to change image size according to my view so I wrote this code for that.

 -(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a bitmap context.
    UIGraphicsBeginImageContextWithOptions(newSize, YES, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    NSData *imageData = UIImagePNGRepresentation(newImage);
    UIGraphicsEndImageContext();


    UIImage *img=[UIImage imageWithData:imageData];

    return img;

}

它工作正常,但当我得到图像时,它的透明部分变成黑色,也许它将它转换成jpg。我使用了PNG图像。有任何想法吗?在此先感谢。

It's working fine, but when I get the image, its transparent part becomes in black, maybe it's converting it into jpg. I used a PNG image. Any ideas? Thanks in advance.

推荐答案

您应该使用作为第二个参数,切换图像的透明度。

You should use NO as the second argument, that toggles the transparency of the image.

UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);

查看 Apple的文档了解更多详情。

这篇关于更改图像尺寸后,图像的透明部分变为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆