UIImage获取灰度图像

来源:互联网 发布:vb与plc以太网通讯 编辑:程序博客网 时间:2024/05/21 08:52

#import <UIKit/UIKit.h>


@interface UIImage(grayImage)



-(UIImage*)getGrayImage;


@end


#import "UIImage+grayImage.h"


@implementation UIImage(grayImage)


-(UIImage*)getGrayImage{

    int width =self.size.width;

    int height =self.size.height;

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceGray();

    CGContextRef context =CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);

    CGColorSpaceRelease(colorSpace);

    if (context ==NULL) {

        returnnil;

    }

    CGContextDrawImage(context,CGRectMake(0,0, width, height), self.CGImage);

    UIImage *grayImage = [UIImageimageWithCGImage:CGBitmapContextCreateImage(context)];

    CGContextRelease(context);

    return grayImage;

}

@end

0 0