如何实现qq头像致灰功能?
//使⽤sdwebimage加载图⽚,之后调⽤致灰⽅法
[_iconImageView sd_setImageWithURL:[NSURL URLWithString:orderItems.iconUrl] placeholderImage:[UIImage imageNamed:@"home_root_headImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
UIImage *grayImage = [self convertToGrayscaleWith:image];
if (grayImage) {
_iconImageView.image = grayImage;
} else {
_iconImageView.image = [UIImage imageNamed:@"home_root_headImage"];
}
}];
//致灰功能的⽅法
- (UIImage *)convertToGrayscaleWith:(UIImage *)scousImage {收银员规章制度
CGSize size = [scousImage size];
掌纹与健康int width = size.width;
int height = size.height;
// the pixels will be painted to this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));
辅导员学期工作总结卜算子咏梅毛译文CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
// paint the bitmap to our context which will fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [scousImage CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
// set the pixels to gray
rgbaPixel[RED] = gray;
rgbaPixel[GREEN] = gray;
rgbaPixel[BLUE] = gray;
}
}
知行合一// create a new CGImageRef from our context with the modified pixels
CGImageRef image = CGBitmapContextCreateImage(context);
// we're done with the context, color space, and pixels
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
// make a new UIImage to return
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
为什么手机网速很慢/
/ we're done with image now too
CGImageRelease(image);
return resultUIImage;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论