首页 > 其他 > 详细

改变图片颜色(灰色显示)

时间:2014-02-08 15:29:32      阅读:430      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
UIImage+grayColor.h

#import <UIKit/UIKit.h>

@interface UIImage (grayColor)

+ (UIImage *)grayImage:(UIImage *)sourceImage;

@end

UIImage+grayColor.m

#import "UIImage+grayColor.h"

@implementation UIImage (grayColor)

+ (UIImage *)grayImage:(UIImage *)sourceImage
{
    int bitmapInfo = kCGImageAlphaNone;
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,
                                                  width,
                                                  height,
                                                  8,      // bits per component
                                                  0,
                                                  colorSpace,
                                                  bitmapInfo);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
    }
    CGContextDrawImage(context,
                       CGRectMake(0, 0, width, height), sourceImage.CGImage);
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    return grayImage;
}

@end

// 使用
 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
 [imageView setImage:[UIImage grayImage:[UIImage imageNamed:@"dark.jpg"]]];
 [self.view addSubview:imageView];
bubuko.com,布布扣

改变图片颜色(灰色显示)

原文:http://www.cnblogs.com/joesen/p/3540125.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!