iOS:OpenGL图片滤镜

参考资料:
https://juejin.im/post/5d1860a2f265da1bb27746d6

并再此基础上添加了其他滤镜:二分、四分、九分、灰度、暖色、冷色、漩涡、矩形马赛克、六边形马赛克、三角形马赛克、圆形马赛克、缩放、闪白、抖动、灵魂出窍、毛刺、幻觉、浮雕。

效果图:


IMG_0800.PNG

也能够在纹理中获取处理后的UIImage

// 获取纹理对应的 UIImage,调用前先绑定对应的帧缓存
- (UIImage *)imageFromTextureWithWidth:(int)width height:(int)height {
    int size = width * height * 4;
    GLubyte *buffer = malloc(size);
    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, size, NULL);
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    
    // 此时的 imageRef 是上下颠倒的,调用 CG 的方法重新绘制一遍,刚好翻转过来
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    free(buffer);
    
    self.imageView.image = image;
    
    return image;
}

Github:
https://github.com/qw9685/OpenGL-imageFilter

你可能感兴趣的:(iOS:OpenGL图片滤镜)