iOS实现图像特效相关


通过慕课网学习了一些图像处理相关的知识点,高大上的特效难点在于算法上,图形处理是小case,好了,talk is cheap, show me the code!


首先需要将 UIImage 转化成 CGImage 数据流:

- (unsigned char*)convertUIImageToData:(UIImage *)image {
    CGImageRef imageref = [image CGImage];
    CGSize image_size = image.size;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *data = malloc(image_size.width*image_size.height*4);

    CGContextRef context = CGBitmapContextCreate(data, image_size.width, image_size.height, 8, 4*image_size.width, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, image_size.width, image_size.height), imageref);

    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);

    return (unsigned char*)data;
}

灰度处理:

- (unsigned char*)imageGrayWithData:(unsigned char*)imageData width:(CGFloat)width height:(CGFloat)height {

    unsigned char *resultData = malloc(width*height*sizeof(unsigned char)*4);
    memset(resultData, 0, width*height*sizeof(unsigned char)*4);
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            unsigned int imageIndex = h*width+w;

            unsigned char bitmapRed = *(imageData+imageIndex*4);
            unsigned char bitmapGreen = *(imageData+imageIndex*4+1);
            unsigned char bitmapBlue = *(imageData+imageIndex*4+2);

            int bitMap = bitmapRed*77/255+bitmapGreen*151/255+bitmapBlue*88/255;
            unsigned char newBitMap = (bitMap>255)?255:bitMap;
            memset(resultData+imageIndex*4, newBitMap, 1);
            memset(resultData+imageIndex*4+1, newBitMap, 1);
            memset(resultData+imageIndex*4+2, newBitMap, 1);
        }
    }
    return resultData;
}

反色处理:

- (unsigned char*)imageReColorWithData:(unsigned char*)imageData  width:(CGFloat)width height:(CGFloat)height {

    unsigned char *resultData = malloc(width*height*sizeof(unsigned char)*4);
    memset(resultData, 0, width*height*sizeof(unsigned char)*4);
    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            unsigned int imageIndex = h*width+w;

            unsigned char bitmapRed = *(imageData+imageIndex*4);
            unsigned char bitmapGreen = *(imageData+imageIndex*4+1);
            unsigned char bitmapBlue = *(imageData+imageIndex*4+2);
            unsigned char bitMapRedNew = 255-bitmapRed;
            unsigned char bitMapGreenNew = 255-bitmapGreen;
            unsigned char bitMapBlueNew = 255-bitmapBlue;

            memset(resultData+imageIndex*4, bitMapRedNew, 1);
            memset(resultData+imageIndex*4+1, bitMapGreenNew, 1);
            memset(resultData+imageIndex*4+2, bitMapBlueNew, 1);
        }
    }
    return resultData;
}

简单的高亮美白处理:

- (unsigned char*)imageHighlightWithData:(unsigned char*)imageData width:(CGFloat)width height:(CGFloat)height {

    unsigned char *resultData = malloc(width*height*sizeof(unsigned char)*4);
    memset(resultData, 0, width*height*sizeof(unsigned char)*4);

    NSArray *colorArrayBase = @[@"55",@"110",@"155",@"185",@"220",@"240",@"250",@"255"];
    NSMutableArray *colorArray = [NSMutableArray new];

    int beforNum = 0;
    for (int i = 0; i < 8; i++) {
        NSString *numStr = [colorArrayBase objectAtIndex:i];
        int num = numStr.intValue;
        float step = 0;
        if (i==0) {
            step = num/32.0;
            beforNum = num;
        } else {
            step = (num-beforNum)/32.0;
        }
        for (int j = 0; j < 32; j++) {
            int newNum = 0;
            if (i==0) {
                newNum = (int)j*step;
            } else {
                newNum = (int)(beforNum+j*step);
            }

            NSString *newNumStr = [NSString stringWithFormat:@"%d",newNum];
            [colorArray addObject:newNumStr];
        }
        beforNum = num;
    }

    for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
            unsigned int imageIndex = h*width+w;

            unsigned char bitmapRed = *(imageData+imageIndex*4);
            unsigned char bitmapGreen = *(imageData+imageIndex*4+1);
            unsigned char bitmapBlue = *(imageData+imageIndex*4+2);

            NSString *redStr = [colorArray objectAtIndex:bitmapRed];
            NSString *greenStr = [colorArray objectAtIndex:bitmapGreen];
            NSString *blueStr = [colorArray objectAtIndex:bitmapBlue];

            unsigned char bitMapRedNew = redStr.intValue;
            unsigned char bitMapGreenNew = greenStr.intValue;
            unsigned char bitMapBlueNew = blueStr.intValue;

            memset(resultData+imageIndex*4, bitMapRedNew, 1);
            memset(resultData+imageIndex*4+1, bitMapGreenNew, 1);
            memset(resultData+imageIndex*4+2, bitMapBlueNew, 1);
        }
    }

    return resultData;
}

将数据流转化回 UIImage:

- (UIImage *)convertDataToUIImage:(unsigned char*)imageData image:(UIImage *)imageSource {

    CGFloat width = imageSource.size.width;
    CGFloat height = imageSource.size.height;
    NSInteger dataLength = width*height*4;

    CGDataProviderRef provide = CGDataProviderCreateWithData(NULL, imageData, dataLength, NULL);
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4*width;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderIntent = kCGRenderingIntentDefault;

    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provide, NULL, NO, renderIntent);

    UIImage *imageNew = [UIImage imageWithCGImage:imageRef];
    CFRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provide);

    return imageNew;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值