| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
d3fw
9年前发布

iOS处理图片颜色代码

处理图片颜色  - (UIImage*)grayscale:(UIImage*)anImage type:(int)type{      CGImageRef imageRef = anImage.CGImage;      size_t width  = CGImageGetWidth(imageRef);      size_t height = CGImageGetHeight(imageRef);      size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);      size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);      size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);      CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);      CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);      bool shouldInterpolate = CGImageGetShouldInterpolate(imageRef);      CGColorRenderingIntent intent = CGImageGetRenderingIntent(imageRef);      CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef);      CFDataRef data = CGDataProviderCopyData(dataProvider);      UInt8 *buffer = (UInt8*)CFDataGetBytePtr(data);      NSUInteger  x, y;      for (y = 0; y < height; y++) {          for (x = 0; x < width; x++) {              UInt8 *tmp;              tmp = buffer + y * bytesPerRow + x * 4;              UInt8 red,green,blue;              red = *(tmp + 0);              green = *(tmp + 1);              blue = *(tmp + 2);              UInt8 brightness;                             switch (type) {                  case 1:                      brightness = (77 * red + 28 * green + 151 * blue) / 256;                      *(tmp + 0) = brightness;                      *(tmp + 1) = brightness;                      *(tmp + 2) = brightness;                      break;                  case 2:                      *(tmp + 0) = red;                      *(tmp + 1) = green * 0.7;                      *(tmp + 2) = blue * 0.4;                      break;                  case 3:                      *(tmp + 0) = 255 - red;                      *(tmp + 1) = 255 - green;                      *(tmp + 2) = 255 - blue;                      break;                  default:                      *(tmp + 0) = red;                      *(tmp + 1) = green;                      *(tmp + 2) = blue;                      break;              }             }      }      CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data));      CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData);      CGImageRef effectedCgImage = CGImageCreate(                                                 width, height,                                                 bitsPerComponent, bitsPerPixel, bytesPerRow,                                                 colorSpace, bitmapInfo, effectedDataProvider,                                                 NULL, shouldInterpolate, intent);      UIImage *effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage];      CGImageRelease(effectedCgImage);      CFRelease(effectedDataProvider);      CFRelease(effectedData);      CFRelease(data);      return effectedImage;  }