如何调整目标C中具有透明部分的图像的大小

时间:2015-07-06 21:57:30

标签: ios objective-c mobile resize uiimage

我正在尝试调整具有透明部分的PNG,首先我使用了:

UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);
[sourceImage  drawInRect:CGRectMake(0,0, newSize.width, newSize.height)];
UIImage* targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但结果图像不透明。然后我读到drawInRect默认情况下将图像绘制为不透明,所以我将drawInRect线修改为:

[fromImage  drawInRect:CGRectMake(0,0, newSize.width, newSize.height) blendMode:kCGBlendModeNormal alpha:0.0];

结果图像是空白的,我认为drawInRect中应该有一些参数组合,它们将保留图像的透明度。

我已经搜索了其他线程,并且在任何地方我都看到了通用的大小调整代码,但它没有涉及带有透明部分的图像。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

CGRect rect = CGRectMake(0,0,newsize.width,newsize.height);
UIGraphicsBeginImageContext( rect.size );
[sourceImage drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageData = UIImagePNGRepresentation(picture1);
textureColor=[UIImage imageWithData:imageData];