ios对图片进行缩小尺寸和透明度设置
2021-06-20 本文已影响0人
guoguojianshu
#import "UIImage+XSScaleImage.h"
@implementation UIImage (XSScaleImage)
-(UIImage *)scaleImageWithSize:(CGSize)size{
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//这里写了一个方法传入需要的透明度和图片
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha image:(UIImage*)image{
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}