1. 程式人生 > 實用技巧 >iOS開發之圖片濾鏡、飽和度等處理

iOS開發之圖片濾鏡、飽和度等處理

1. 照片變黑白(遍歷畫素)

    CGImageRef imageRef = self.imageView.image.CGImage;
    // 一個位元組 = 8bit(位) 每行有 17152 個位元組,即每行有 17152 * 8 個位   一個畫素有 4 個位元組
    size_t width = CGImageGetWidth(imageRef);
    size_t height = CGImageGetHeight(imageRef);
    size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
    size_t bytesPerRow 
= CGImageGetBytesPerRow(imageRef); // 顏色空間 uint32_t bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef space = CGImageGetColorSpace(imageRef); //獲取每個畫素的位元組資料集合 CGDataProviderRef dataProviderRef = CGImageGetDataProvider(imageRef); CFDataRef dataRef = CGDataProviderCopyData(dataProviderRef); UInt8
* bytesPtr = CFDataGetMutableBytePtr((CFMutableDataRef)dataRef); NSUInteger length = CFDataGetLength(dataRef); // 一個畫素由四個位元組byte組成 for (int i = 0; i < length; i += 4) { UInt8 red = bytesPtr[i]; UInt8 green = bytesPtr[i + 1]; UInt8 blue = bytesPtr[i + 2]; UInt8 average
= (red + green + blue) / 3; bytesPtr[i] = average; bytesPtr[i + 1] = average; bytesPtr[i + 2] = average; } CGContextRef imageContextRef = CGBitmapContextCreate(bytesPtr, width, height, bitsPerComponent, bytesPerRow, space, bitmapInfo); CGImageRef finalImageRef = CGBitmapContextCreateImage(imageContextRef); UIImage * finalImage = [UIImage imageWithCGImage:finalImageRef]; self.imageView.image = finalImage; CGImageRelease(finalImageRef); CGContextRelease(imageContextRef); CFRelease(dataRef);

2. 照片飽和度調整

    CIImage *beginImage = [CIImage imageWithCGImage:self.imageView.image.CGImage];
    CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];
    [filter setValue:beginImage forKey:kCIInputImageKey];
    //飽和度 0---2 預設為1
    [filter setValue:[NSNumber numberWithFloat:self.slider.value*2] forKey:@"inputSaturation"];
    
    // 得到過濾後的圖片
    CIImage *outputImage = [filter outputImage];
    // 轉換圖片, 建立基於GPU的CIContext物件
    CIContext *context = [CIContext contextWithOptions: nil];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];
    // 顯示圖片
    [self.imageView setImage:newImg];
    // 釋放C物件
    CGImageRelease(cgimg);

3. 照片亮度調整

    CIImage *beginImage = [CIImage imageWithCGImage:self.imageView.image.CGImage];
    CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];
    [filter setValue:beginImage forKey:kCIInputImageKey];
    //亮度 -1---1 預設為0
    [filter setValue:[NSNumber numberWithFloat:(self.slider.value-0.5)*2] forKey:@"inputBrightness"];
    
    // 得到過濾後的圖片
    CIImage *outputImage = [filter outputImage];
    // 轉換圖片, 建立基於GPU的CIContext物件
    CIContext *context = [CIContext contextWithOptions: nil];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];
    // 顯示圖片
    [self.imageView setImage:newImg];
    // 釋放C物件
    CGImageRelease(cgimg);

4. 照片對比度調整

    CIImage *beginImage = [CIImage imageWithCGImage:self.imageView.image.CGImage];
    CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];
    [filter setValue:beginImage forKey:kCIInputImageKey];
    //對比度 0---4 預設為1
    [filter setValue:[NSNumber numberWithFloat:self.slider.value*4.00f] forKey:@"inputContrast"];
    
    // 得到過濾後的圖片
    CIImage *outputImage = [filter outputImage];
    // 轉換圖片, 建立基於GPU的CIContext物件
    CIContext *context = [CIContext contextWithOptions: nil];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];
    // 顯示圖片
    [self.imageView setImage:newImg];
    // 釋放C物件
    CGImageRelease(cgimg);

5. 照片新增純色濾鏡

    UIGraphicsBeginImageContext(CGSizeMake(self.imageView.image.size.width, self.imageView.image.size.height));
    CGContextRef imageContextRef = UIGraphicsGetCurrentContext();
    [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height)];
    //紅色濾鏡
    UIColor *color = [UIColor clearColor];
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0://
            color = [UIColor colorWithRed:1 green:0 blue:0 alpha:self.slider.value];
            break;
        case 1://
            color = [UIColor colorWithRed:0 green:1 blue:0 alpha:self.slider.value];
            break;
        case 2://
            color = [UIColor colorWithRed:0 green:0 blue:1 alpha:self.slider.value];
            break;
        default:
            break;
    }
    // 給上下文設定顏色
    CGContextSetFillColorWithColor(imageContextRef, color.CGColor);
    // 設定渲染模式
    CGContextSetBlendMode(imageContextRef, kCGBlendModeNormal);
    // 填充上下文
    CGContextFillRect(imageContextRef, CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height));
    CGImageRef finalImageRef = CGBitmapContextCreateImage(imageContextRef);
    UIImage * finalImage = [UIImage imageWithCGImage:finalImageRef];
    self.imageView.image = finalImage;
    
    CGImageRelease(finalImageRef);
    CGContextRelease(imageContextRef);

參考自:https://www.jianshu.com/p/d375432ebb0d