美文网首页
ios对图片进行缩小尺寸和透明度设置

ios对图片进行缩小尺寸和透明度设置

作者: guoguojianshu | 来源:发表于2021-06-20 10:03 被阅读0次
    #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;
    }
    

    相关文章

      网友评论

          本文标题:ios对图片进行缩小尺寸和透明度设置

          本文链接:https://www.haomeiwen.com/subject/eeijyltx.html