更改图像尺寸后,图像的透明部分变为黑色
问题描述:
我正在尝试根据我的视图更改图像大小,所以我为此编写了此代码。
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的文档了解更多详情。