美文网首页
UIImage 中可能用到的一些category

UIImage 中可能用到的一些category

作者: c_f | 来源:发表于2017-06-08 14:27 被阅读0次

    1、改变图片颜色

    //改变图片颜色
    - (UIImage *)imageWithColor:(UIColor *)color
    {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        CGContextRef   context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, self.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGRect   rect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGContextClipToMask(context, rect, self.CGImage);
        [color setFill];
        CGContextFillRect(context, rect);
        UIImage  *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    2、获取image中指定区域的颜色

    - (UIColor *)getPixelColorAtLocation:(CGPoint)point {
        UIColor* color = nil;
        CGImageRef inImage = [self CGImage];
        CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
        if (cgctx == NULL) { return nil;  }
        size_t w = CGImageGetWidth(inImage);
        size_t h = CGImageGetHeight(inImage);
        CGRect rect = {{0,0},{w,h}};
        CGContextDrawImage(cgctx, rect, inImage);
        unsigned char* data = CGBitmapContextGetData (cgctx);
        if (data != NULL) {
            @try {
                int offset = 4*((w*round(point.y))+round(point.x));
                int alpha =  data[offset];
                int red = data[offset+1];
                int green = data[offset+2];
                int blue = data[offset+3];
                color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
            }
            @catch (NSException* e) {
                NSLog(@"%@",[e reason]);
            }
            @finally {
            }
        }
        CGContextRelease(cgctx);
        if (data) { free(data); }
        return color;
    }
    - (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef) inImage {
        CGContextRef    context = NULL;
        CGColorSpaceRef colorSpace;
        void *          bitmapData;
        int             bitmapByteCount;
        int             bitmapBytesPerRow;
        size_t pixelsWide = CGImageGetWidth(inImage);
        size_t pixelsHigh = CGImageGetHeight(inImage);
        bitmapBytesPerRow   = (int)(pixelsWide * 4);
        bitmapByteCount     = (int)(bitmapBytesPerRow * pixelsHigh);
        colorSpace = CGColorSpaceCreateDeviceRGB();
        if (colorSpace == NULL)
        {
            fprintf(stderr, "Error allocating color space\n");
            return NULL;
        }
        bitmapData = malloc( bitmapByteCount );
        if (bitmapData == NULL)
        {
            fprintf (stderr, "Memory not allocated!");
            CGColorSpaceRelease( colorSpace );
            return NULL;
        }
        context = CGBitmapContextCreate (bitmapData,
                                         pixelsWide,
                                         pixelsHigh,
                                         8,      
                                         bitmapBytesPerRow,
                                         colorSpace,
                                         kCGImageAlphaPremultipliedFirst);
        if (context == NULL)
        {
            free (bitmapData);
            fprintf (stderr, "Context not created!");
        }
        CGColorSpaceRelease( colorSpace );
        return context;
    }
    

    3、设置图片圆角

    //设置图片圆角
    - (UIImage *)circleImage {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGContextAddArc(ctx, self.size.width/2.0, self.size.height/2.0, (self.size.width/2.0)>(self.size.height/2.0)?(self.size.height/2.0):(self.size.width/2.0), 0, M_PI*2, 1);
        CGContextClip(ctx);
        [self drawInRect:rect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    相关文章

      网友评论

          本文标题:UIImage 中可能用到的一些category

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