iOS:OpenGL圖片濾鏡

參考資料:
https://juejin.im/post/5d1860a2f265da1bb27746d6

并再此基礎(chǔ)上添加了其他濾鏡:二分、四分、九分、灰度、暖色、冷色、漩渦、矩形馬賽克、六邊形馬賽克、三角形馬賽克、圓形馬賽克、縮放、閃白、抖動(dòng)、靈魂出竅、毛刺、幻覺(jué)、浮雕。

效果圖:


IMG_0800.PNG

也能夠在紋理中獲取處理后的UIImage

// 獲取紋理對(duì)應(yīng)的 UIImage,調(diào)用前先綁定對(duì)應(yīng)的幀緩存
- (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);
    
    // 此時(shí)的 imageRef 是上下顛倒的,調(diào)用 CG 的方法重新繪制一遍,剛好翻轉(zhuǎn)過(guò)來(lái)
    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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容