iOS給圖片添加濾鏡&使用openGLES動態渲染圖片詳解及執行個體_IOS

來源:互聯網
上載者:User

iOS給圖片添加濾鏡&使用openGLES動態渲染圖片

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

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

#1.匯入CIImage格式的原始圖片

#2.建立CIFilter濾鏡

#3.用CIContext將濾鏡中的圖片渲染出來

#4.匯出渲染後的圖片

參考代碼:

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

當要設定多個濾鏡的時候, 出了新建立一個CIFilter外還要額外設定kCIInputAngleKey, 代碼如下:

//匯入CIImage  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua.jpeg"]];    //建立出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]; //如果不增加這行新增的濾鏡不會生效    CIImage *outputImage = [filterTwo valueForKey:kCIOutputImageKey];    //用CIContext將濾鏡中的圖片渲染出來  CIContext *context = [CIContext contextWithOptions:nil];     CGImageRef cgImage = [context createCGImage:outputImage                    fromRect:[outputImage extent]];    //匯出圖片  UIImage *showImage = [UIImage imageWithCGImage:cgImage];    CGImageRelease(cgImage);    UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage];  imageView.center    = self.view.center;  [self.view addSubview:imageView];

下面來介紹怎麼用openGLES來使用濾鏡渲染圖片

使用openGlES的步驟大致如下:

#1.匯入要渲染的圖片

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

#3.建立出渲染的GLKView buffer

#4.建立CoreImage的上下文

#5.進行CoreImage的相關設定

#6.開始渲染並顯示圖片

參考代碼如下:

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

如果要動態渲染, 可以通過UISilder動態調整一下代碼的vaule值

[filter setValue:vaule forKey:kCIInputIntensityKey];

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.