国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例

2020-07-26 03:07:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片

給圖片增加濾鏡有這兩種方式: CoreImage / openGLES

 下面先說(shuō)明如何使用CoreImage給圖片添加濾鏡, 主要為以下步驟:

#1.導(dǎo)入CIImage格式的原始圖片

#2.創(chuàng)建CIFilter濾鏡

#3.用CIContext將濾鏡中的圖片渲染出來(lái)

#4.導(dǎo)出渲染后的圖片

參考代碼:

//導(dǎo)入CIImage  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua"]];    //創(chuàng)建出Filter濾鏡  CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"];    [filter setValue:ciImage forKey:kCIInputImageKey];    [filter setDefaults];    CIImage *outImage = [filter valueForKey:kCIOutputImageKey];    //用CIContext將濾鏡中的圖片渲染出來(lái)  CIContext *context = [CIContext contextWithOptions:nil];    CGImageRef cgImage = [context createCGImage:outImage                    fromRect:[outImage extent]];    //導(dǎo)出圖片  UIImage *showImage = [UIImage imageWithCGImage:cgImage];    CGImageRelease(cgImage);    UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage];  imageView.center    = self.view.center;  [self.view addSubview:imageView];

當(dāng)要設(shè)置多個(gè)濾鏡的時(shí)候, 出了新創(chuàng)建一個(gè)CIFilter外還要額外設(shè)定kCIInputAngleKey, 代碼如下:

//導(dǎo)入CIImage  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua.jpeg"]];    //創(chuàng)建出Filter濾鏡  CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"];    [filter setValue:ciImage forKey:kCIInputImageKey];    [filter setDefaults];    CIImage *outImage = [filter valueForKey:kCIOutputImageKey];    CIFilter *filterTwo = [CIFilter filterWithName:@"CIHueAdjust"];    [filterTwo setValue:outImage forKey:kCIInputImageKey];    [filterTwo setDefaults];    [filterTwo setValue:@(1.0f) forKey:kCIInputAngleKey]; //如果不增加這行新增的濾鏡不會(huì)生效    CIImage *outputImage = [filterTwo valueForKey:kCIOutputImageKey];    //用CIContext將濾鏡中的圖片渲染出來(lái)  CIContext *context = [CIContext contextWithOptions:nil];     CGImageRef cgImage = [context createCGImage:outputImage                    fromRect:[outputImage extent]];    //導(dǎo)出圖片  UIImage *showImage = [UIImage imageWithCGImage:cgImage];    CGImageRelease(cgImage);    UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage];  imageView.center    = self.view.center;  [self.view addSubview:imageView];

下面來(lái)介紹怎么用openGLES來(lái)使用濾鏡渲染圖片

使用openGlES的步驟大致如下:

#1.導(dǎo)入要渲染的圖片

#2.獲取OpenGLES渲染的上下文

#3.創(chuàng)建出渲染的GLKView buffer

#4.創(chuàng)建CoreImage的上下文

#5.進(jìn)行CoreImage的相關(guān)設(shè)置

#6.開(kāi)始渲染并顯示圖片

參考代碼如下:

//導(dǎo)入要渲染的圖片  UIImage *showImage = [UIImage imageNamed:@"hua.jpeg"];  CGRect rect    = CGRectMake(0, 0, showImage.size.width, showImage.size.height);    //獲取OpenGLES渲染的上下文  EAGLContext *eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];    //創(chuàng)建出渲染的buffer  GLKView *glkView = [[GLKView alloc] initWithFrame:rect                       context:eagContext];  [glkView bindDrawable];  [self.view addSubview:glkView];    //創(chuàng)建出CoreImage的上下文  CIContext *ciContext = [CIContext contextWithEAGLContext:eagContext                           options:@{kCIContextWorkingColorSpace: [NSNull null]}];    //CoreImage相關(guān)設(shè)置  CIImage *ciImage = [[CIImage alloc] initWithImage:showImage];    CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];    [filter setValue:ciImage forKey:kCIInputImageKey];  [filter setValue:@(0) forKey:kCIInputIntensityKey];    //開(kāi)始渲染  [ciContext drawImage:[filter valueForKey:kCIOutputImageKey]         inRect:CGRectMake(0, 0, glkView.drawableWidth, glkView.drawableHeight)        fromRect:[ciImage extent]];    [glkView display];

如果要?jiǎng)討B(tài)渲染, 可以通過(guò)UISilder動(dòng)態(tài)調(diào)整一下代碼的vaule值

[filter setValue:vaule forKey:kCIInputIntensityKey];

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 四子王旗| 肇庆市| 庄河市| 平南县| 武功县| 宣化县| 呼伦贝尔市| 合江县| 乌拉特前旗| 桃江县| 石渠县| 南和县| 巩留县| 讷河市| 弋阳县| 萍乡市| 宁陕县| 阿克| 昌吉市| 奇台县| 灵寿县| 岱山县| 交城县| 清新县| 县级市| 甘孜县| 磴口县| 马山县| 久治县| 贵州省| 望城县| 九龙坡区| 镇巴县| 南京市| 额尔古纳市| 罗定市| 仲巴县| 大新县| 郯城县| 肇源县| 三明市|