iOS实现图像特效相关

来源:互联网 发布:天宇水利预算软件 编辑:程序博客网 时间:2024/06/06 02:32

通过慕课网学习了一些图像处理相关的知识点,高大上的特效难点在于算法上,图形处理是小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;}