iOS学习----------图片的简单处理(2)

本文介绍了一种在iOS平台实现图像叠加与黑白转换的技术方案。具体步骤包括:调整幽灵图层大小及位置、获取像素并按50%透明度混合、最后将图像转为灰度显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接第一部分内容

 

//图片的大小

      CGSize ghostSize = CGSizeMake(targetGhostWidth, targetGhostWidth / ghostImageAspectRatio);

    

    //图片的位置

      CGPoint ghostOrigin = CGPointMake(inputWidth * 0.5, inputHeight * 0.2);

      

      // 2.2 Scale & Get pixels of the ghost

      NSUInteger ghostBytesPerRow = bytesPerPixel * ghostSize.width;

      

      UInt32 * ghostPixels = (UInt32 *)calloc(ghostSize.width * ghostSize.height, sizeof(UInt32));

      

      CGContextRef ghostContext = CGBitmapContextCreate(ghostPixels, ghostSize.width, ghostSize.height,

                                                        bitsPerComponent, ghostBytesPerRow, colorSpace,

                                                        kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

      

      CGContextDrawImage(ghostContext, CGRectMake(0, 0, ghostSize.width, ghostSize.height),ghostCGImage);

    

      // 2.3 Blend each pixel

      NSUInteger offsetPixelCountForInput = ghostOrigin.y * inputWidth + ghostOrigin.x;

      for (NSUInteger j = 0; j < ghostSize.height; j++) {

        for (NSUInteger i = 0; i < ghostSize.width; i++) {

          UInt32 * inputPixel = inputPixels + j * inputWidth + i + offsetPixelCountForInput;

          UInt32 inputColor = *inputPixel;

          

          UInt32 * ghostPixel = ghostPixels + j * (int)ghostSize.width + i;

          UInt32 ghostColor = *ghostPixel;

          

          // Blend the ghost with 50% alpha

          CGFloat ghostAlpha = 0.5f * (A(ghostColor) / 255.0);

          UInt32 newR = R(inputColor) * (1 - ghostAlpha) + R(ghostColor) * ghostAlpha;

          UInt32 newG = G(inputColor) * (1 - ghostAlpha) + G(ghostColor) * ghostAlpha;

          UInt32 newB = B(inputColor) * (1 - ghostAlpha) + B(ghostColor) * ghostAlpha;

          

          //Clamp, not really useful here :p

          newR = MAX(0,MIN(255, newR));

          newG = MAX(0,MIN(255, newG));

          newB = MAX(0,MIN(255, newB));

          

          *inputPixel = RGBAMake(newR, newG, newB, A(inputColor));

        }

      }

      

      // 3. Convert the image to Black & White

      for (NSUInteger j = 0; j < inputHeight; j++) {

        for (NSUInteger i = 0; i < inputWidth; i++) {

          UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;

          UInt32 color = *currentPixel;

          

          // Average of RGB = greyscale

          UInt32 averageColor = (R(color) + G(color) + B(color)) / 3.0;

          

          *currentPixel = RGBAMake(averageColor, averageColor, averageColor, A(color));

        }

      }


      // 4. Create a new UIImage

    //context中画好的图片 转化为CGImage

      CGImageRef newCGImage = CGBitmapContextCreateImage(context);

      UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];

      

      // 5. Cleanup!

      CGColorSpaceRelease(colorSpace);

      CGContextRelease(context);

      CGContextRelease(ghostContext);

      free(inputPixels);

      free(ghostPixels);

      

      return processedImage;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值