美文网首页
(转)UIImage图片处理(更改颜色,修改透明度,缩小,合并图

(转)UIImage图片处理(更改颜色,修改透明度,缩小,合并图

作者: funnythansl | 来源:发表于2017-05-18 11:00 被阅读197次

    转自:http://blog.sina.com.cn/s/blog_64cfe8f00101pm3x.html
    //遍历图片像素,更改图片颜色
    void ProviderReleaseData (void *info, const void data, size_t size)
    {
    free((void
    )data);
    }

    • (UIImage) imageBlackToTransparent:(UIImage) image
      {
      // 分配内存
      const int imageWidth = image.size.width;
      const int imageHeight = image.size.height;
      size_t bytesPerRow = imageWidth * 4;
      uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);

      // 创建context
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,
      kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
      CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

      // 遍历像素
      int pixelNum = imageWidth * imageHeight;
      uint32_t* pCurPtr = rgbImageBuf;
      for (int i = 0; i < pixelNum; i++, pCurPtr++)
      {
      if ((pCurPtr & 0xFFFFFF00) == 0xffffff00) // 将白色变成透明
      {
      uint8_t
      ptr = (uint8_t)pCurPtr;
      ptr[0] = 0;
      }
      else
      {
      // 改成下面的代码,会将图片转成想要的颜色
      uint8_t
      ptr = (uint8_t*)pCurPtr;
      ptr[3] = 0; //0~255
      ptr[2] = 0;
      ptr[1] = 0;

        }
      

      }

      // 将内存转成image
      CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
      CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace,
      kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,
      NULL, true, kCGRenderingIntentDefault);
      CGDataProviderRelease(dataProvider);

      UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];

      // 释放
      CGImageRelease(imageRef);
      CGContextRelease(context);
      CGColorSpaceRelease(colorSpace);
      // free(rgbImageBuf) 创建dataProvider时已提供释放函数,这里不用free

      return resultUIImage;
      }

    //设置图片透明度

    • (UIImage )imageByApplyingAlpha:(CGFloat)alpha image:(UIImage)image
      {
      UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);

      CGContextRef ctx = UIGraphicsGetCurrentContext();
      CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);

      CGContextScaleCTM(ctx, 1, -1);
      CGContextTranslateCTM(ctx, 0, -area.size.height);

      CGContextSetBlendMode(ctx, kCGBlendModeMultiply);

      CGContextSetAlpha(ctx, alpha);

      CGContextDrawImage(ctx, area, image.CGImage);

      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

      UIGraphicsEndImageContext();

      return newImage;
      }

    //合并2张图片

    • (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
      UIGraphicsBeginImageContext(image2.size);

      // Draw image1
      [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

      // Draw image2
      [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

      UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return resultingImage;
      }

    //将UIImage缩放到指定大小尺寸:

    • (UIImage *)scaleToSize:(UIImage )img size:(CGSize)size{
      // 创建一个bitmap的context
      // 并把它设置成为当前正在使用的context
      UIGraphicsBeginImageContext(size);
      // 绘制改变大小的图片
      [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
      // 从当前context中创建一个改变大小后的图片
      UIImage
      scaledImage = UIGraphicsGetImageFromCurrentImageContext();
      // 使当前的context出堆栈
      UIGraphicsEndImageContext();
      // 返回新的改变大小后的图片
      return scaledImage;
      }

    //根据图片的大小等比例压缩返回图片
    -(UIImage *)fitSmallImage:(UIImage *)image size:(CGSize)size
    {
    if (nil == image)
    {
    return nil;
    }
    if (image.size.width<<span class="s5">200 && image.size.height<<span class="s5">200)
    {
    return image;
    }
    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [image drawInRect:rect];
    UIImage *newing = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newing;
    }

    相关文章

      网友评论

          本文标题:(转)UIImage图片处理(更改颜色,修改透明度,缩小,合并图

          本文链接:https://www.haomeiwen.com/subject/vncsxxtx.html