iOS 获取view上某一点的色值

来源:互联网 发布:纺织英语翻译软件 编辑:程序博客网 时间:2024/05/22 16:47

//

//  UIView+ColorView.m

//  CAEmitterCell

//

//  Created by Holly on 2017/11/30.

//  Copyright © 2017年 Holly. All rights reserved.

//


#import "UIView+ColorView.h"

#import <QuartzCore/QuartzCore.h>


@implementation UIView (ColorView)

-(UIColor *)colorOfPoint:(CGPoint)point {

    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

    CGContextRef context =CGBitmapContextCreate(pixel,1,1,8,4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    

    CGContextTranslateCTM(context, -point.x, -point.y);

    

    [self.layerrenderInContext:context];

    

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    

    UIColor *color = [UIColorcolorWithRed:pixel[0]/255.0green:pixel[1]/255.0blue:pixel[2]/255.0alpha:pixel[3]/255.0];

    

    return color;

}


@end


参考:http://www.cnblogs.com/Free-Thinker/p/5105900.html

阅读全文
0 0