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

效果图

56.png

学习在iOS中处理图像和创建酷炫的效果!

第一种方法:获取每个像素将两张图片合并

- (UIImage *)processUsingPixels:(UIImage*)inputImage

{

    

      // 1. Get the raw pixels of the image

     UInt32 * inputPixels;//定义一个整型的指针

      

     CGImageRef inputCGImage = [inputImageCGImage];

     NSUInteger inputWidth =CGImageGetWidth(inputCGImage);

     NSUInteger inputHeight =CGImageGetHeight(inputCGImage);

      

    //创建一个RGB模式的颜色空间CGColorSpace和一个容器CGBitmapContext,将像素指针参数传递到容器中缓存进行存储

    

    

   /*

     参数bytesPerPixel(每像素大小)和bitsPerComponent(每个颜色通道大小)

     */

     NSUInteger bytesPerPixel =4;

     NSUInteger bitsPerComponent =8;

    

    //输入的每行

     NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;// 4*3000

     //calloc在动态分配完内存后,自动初始化该内存空间为零

    //初始化一个二维整型数组 inputHeight行(row inputWidth列(col 数组中每个对象的为UInt32的大小

      inputPixels = (UInt32 *)calloc(inputHeight * inputWidth,sizeof(UInt32));

    

    

    //创建一个RGB模式的颜色空间CGColorSpace和一个容器CGBitmapContext,将像素指针参数传递到容器中缓存进行存储

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();


   /*

     CGContextRef __nullable CGBitmapContextCreate(void * __nullable data,

     size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow,

     CGColorSpaceRef __nullable space, uint32_t bitmapInfo)

     */

   CGContextRef context =CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,

                                                   bitsPerComponent, inputBytesPerRow, colorSpace,

                                                  kCGImageAlphaPremultipliedLast |kCGBitmapByteOrder32Big);

    

   //将设定好的图片绘制在上下文(context)中

     CGContextDrawImage(context,CGRectMake(0,0, inputWidth, inputHeight), inputCGImage);

      

        

        

      // 2. Blend the ghost onto the image

     UIImage * ghostImage = [UIImageimageNamed:@"ghost"];

     CGImageRef ghostCGImage = [ghostImageCGImage];

      

      // 2.1 Calculate the size & position of the ghost

    //宽高比

     CGFloat ghostImageAspectRatio = ghostImage.size.width / ghostImage.size.height;

    //宽度等于低部图片的1/4

     NSInteger targetGhostWidth = inputWidth *0.25;

 //

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值