iOS图片叠加
2017-10-13 本文已影响0人
GTReload
在做QQ、微信分享时,需要做图片叠加。因服务端返回的图片一般是jpg(1x),和本地图片的scale(2x或3x)不一致;大小也可能不一致,做相应等比的放大缩小。
//jpg的scale是1x,一般以本地的scale为准,本地和网络图宽高比一致
- (UIImage *)componseImage:(UIImage *)netImg localImage:(UIImage *)localImg {
UIGraphicsBeginImageContextWithOptions(localImg.size, NO, localImg.scale);
netImg = [self scaleImage:netImg widthScale:localImg.size.width/netImg.size.width heightScale:localImg.size.height/netImg.size.height imgScale:localImg.scale];
[netImg drawInRect:CGRectMake(0, 0, netImg.size.width, netImg.size.height)];
[localImg drawInRect:CGRectMake(0, 0, localImg.size.width, localImg.size.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
- (UIImage *)scaleImage:(UIImage *)image
widthScale:(float)widthScale
heightScale:(float)heightScale
imgScale:(CGFloat)imgScale
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width * widthScale, image.size.height * heightScale), NO, imgScale);
[image drawInRect:CGRectMake(0, 0, image.size.width * widthScale, image.size.height * heightScale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}