How to select an image filter for iPhone

Source: Internet
Author: User
Tags image filter

Fixed Link:
Http://www.zhouxiaodong.net /? P = 66 | Station of the melancholy little cave

How to select an image filter for iPhone

First of all, when you are doing a selfie software, this function will be involved, that is, adding a filter to the image, such as black and white, retro and other effects, to make the photo show different effects. The following code shows how to implement this function. Of course, you can add your own effect,

Implementation results

# Import <uikit/uikit. h> # import <quartzcore/quartzcore. h> @ interface photoselectviewcontroller: uiviewcontroller {iboutlet uiimageview * imageview; // iboutlet uiscrollview * scrollerview; // The scroll view displays all the filter image uiimage * theimage; // The Int selectindex of the original image; // The number of images selected} @ end

/****** Various color settings ******* // lomoconst float colormatrix_lomo [] = {1.7f, 0.1f, 0.1f, 0,-73.1f, 0, 1.7f, 0.1f, 0,-73.1f, 0, 0.1f, 1.6f, 0,-73.1f, 0, 0, 0, 1.0f, 0 }; // const float colormatrix_heibai [] = {0.8f, 1.6f, 0.2f, 0,-163.9f, 0.8f, 1.6f, 0.2f, 0,-163.9f, 0.8f, 1.6f, 0.2f, 0, -163.9f, 0, 0, 0, 1.0f, 0}; // retro const float colormatrix_1_jiu [] = {0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0. 2f, 0.5f, 0.1f, 0, 40.8f, 0, 0, 0, 1, 0}; // Gothic const float colormatrix_gete [] = {1.9f,-0.3f,-0.2f, 0,-87.0f,-0.2f, 1.7f,-0.1f, 0,-87.0f,-0.1f,-0.6f, 2.0f, 0,-87.0f, 0, 0, 0, 1.0f, 0}; // sharpen const float colormatrix_ruise [] = {4.8f,-1.0f,-0.1f, 0,-388.4f,-0.5f, 4.4f,-0.1f, 0,-388.4f, -0.5f,-1.0f, 5.2f, 0,-388.4f, 0, 0, 0, 1.0f, 0}; // elegant const float colormatrix_danya [] = {0.6f, 0.3f, 0.1f, 0, 73.3f, 0.2f, 0.7f, 0.1f, 0, 73.3f, 0.2f, 0.3f, 0.4f, 0, 73.3f, zero, 0, 0, 1.0f, 0 }; // wine red const float character [] = {1.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, Hangzhou, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f, 0, 0, 0, 1.0f, 0}; // qingning const float colormatrix_qingning [] = {0.9f, 0, 0, 0, 1.1f, 0, 0, 0, 0, 0.9f, 0, 0, 0, 0, 0, 1.0f, 0}; // romantic const float colormatrix_langman [] = {0.9f, 0, 0, 0, 63.0f, 0, 0.9f, 0, 0, 63.0 F, 0, 0, 0.9f, 0, 63.0f, 0, 0, 0, 1.0f, 0}; // halo const float colormatrix_guangyun [] = {0.9f, 0, 0, 0, 64.9f, 0, 0.9f, 0, 0, 64.9f, 0, 0, 0.9f, 0, 64.9f, 0, 0, 0, 1.0f, 0 }; // blues const float colormatrix_landiao [] = {2.1f,-1.4f, 0.6f, 0.0f,-31.0f,-0.3f, 2.0f,-0.3f, 0.0f,-31.0f,-1.1f, -0.2f, 2.6f, 0.0f,-31.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}; // dream const float colormatrix_menghuan [] = {0.8f, 0.3f, 0.1f, 0.0 F, 46.5f, 0.1f, 0.9f, 0.0f, 0.0f, 46.5f, 0.1f, 0.3f, 0.7f, 0.0f, 46.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; // night const float labels [] = {1.0f, 0.0f, 0.0f, 0.0f,-66.6f, 0.0f, 1.1f, 0.0f, 0.0f,-66.6f, 0.0f, 0.0f, 1.0f, 0.0f, -66.6f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}; # import "photoselectviewcontroller. H "@ interface photoselectviewcontroller () @ end @ implementation photoselectviewcontroller-(ID) initwit Hnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: role]; If (Self) {// custom initialization} return self ;} -(void) viewdidload {[Super viewdidload]; theimage = [uiimage imagenamed: @ "photo.png"]; imageview. image = theimage; // Add the filter nsarray * arr = [nsarray arraywithobjects: @ "source image", @ "LoMo", @ "Black and White", @ "Retro ", @ "goth", @ "red", @ "elegant", @ "wine red", @ "qingting", @ "Romantic", @ "Halo", @ "Blues", @ "dream", @ "night", nil]; // sets the scrollerview attribute of the rolling video. backgroundcolor = [uicolor colorwithred: 0 Green: 0 Blue: 0 ALPHA: 0.5]; scrollerview. indicatorstyle = uiscrollviewindicatorstyleblack; scrollerview. showshorizontalscrollindicator = no; scrollerview. showsverticalscrollindicator = no; // close the scroll bar scrollerview. bounces = no; // Add all the displays to the float X in the scrollerview; // calculate the X coordinate for (INT I = 0; I <arr. count; I ++) {x = 5 + 51 * I; // Add click Gesture Recognition to process the selected image watermark * recognizer = [[uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (setimagestyle :)]; recognizer. numberoftouchesrequired = 1; recognizer. numberoftapsrequired = 1; // Add the name label uilabel * label = [[uilabel alloc] initwithframe: cgrectmake (x, 53, 40, 23)]; [label setbackgroundcolor: [uicolor clearcolor]; [label settext: [arr objectatindex: I]; [label Settextalignment: tags]; [label setfont: [uifont systemfontofsize: 13.0f]; [label settextcolor: [uicolor whitecolor]; [label setuserinteractionenabled: Yes]; [label settag: i]; [scrollerview addsubview: Label]; // Add an int tag = I + 1000; uiimageview * bgimageview = [[uiimageview alloc] initwithframe: cgrectmake (x, 10, 43, 43)]; [bgimageview settag: Tag]; [bgimageview addgesturerecognizer: recognizer]; [Bgimageview setuserinteractionenabled: Yes]; uiimage * bgimage = [self changeimage: bgimageview. tag imageview: Nil]; bgimageview. image = bgimage; [scrollerview addsubview: bgimageview]; [bgimageview setbackgroundcolor: [uicolor redcolor]; // import <quartzcore/quartzcore. h> // Add the selected border. The selected border is yellow. The default is the first selected if (I = 0) {calayer * layer = [bgimageview layer]. layer. bordercolor = [[uicolor yellowcolor] cgcolor]; Layer. Borderwidth = 1.0f; selectindex = tag;} else {calayer * layer = [bgimageview layer]; layer. bordercolor = [[uicolor blackcolor] cgcolor]; layer. borderwidth = 1.0f ;}// you can specify the actual size of the rolling view. contentsize = cgsizemake (x + 55, 60);} // select Image Processing Method-(ibaction) setimagestyle :( uitapgesturerecognizer *) sender {// obtain uiimage * image = [self changeimage: sender. view. tag imageview: Nil]; If (selectindex! = Sender. view. tag) {// deselect the border uiimageview * noselectimageview = (uiimageview *) [self. view viewwithtag: selectindex]; calayer * nolayer = [noselectimageview layer]; nolayer. bordercolor = [[uicolor blackcolor] cgcolor]; nolayer. borderwidth = 1.0f; selectindex = sender. view. tag; uiimageview * selectimageview = (uiimageview *) [self. view viewwithtag: selectindex]; // Add the selection border calayer * layer = [selectimageview layer]; la Yer. bordercolor = [[uicolor yellowcolor] cgcolor]; layer. borderwidth = 1.0f;} // set the image to [imageview setimage: Image];} // select-(uiimage *) changeimage :( INT) index imageview :( uiimageview *) imageview {uiimage * image; Switch (index-1000) {Case 0: {return theimage;} break; Case 1: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_lomo];} break; Case 2: {image = [self imagewithimage: theimage withcolormat Rix: colormatrix_heibai];} break; Case 3: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_huajiu];} break; Case 4: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_gete];} break; Case 5: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_ruise];} break; Case 6: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_danya];} break; Case 7: {image = [Self imagewithimage: theimage withcolormatrix: colormatrix_jiuhong];} break; case 8: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_qingning];} break; Case 9: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_langman];} break; case 10: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_guangyun];} break; Case 11: {image = [self imagewithimage: theimage withcol Ormatrix: colormatrix_landiao];} break; Case 12: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_menghuan];} break; Case 13: {image = [self imagewithimage: theimage withcolormatrix: colormatrix_yese] ;}} return image ;}//-(uiimage *) imagewithimage :( uiimage *) inimage withcolormatrix :( const float *) f {unsigned char * imgpixel = requestimagepixeldata (inimage ); cgimageref inimageref = [inimage C Gimage]; gluint W = cgimagegetwidth (inimageref); gluint H = cgimagegetheight (inimageref); int woff = 0; int pixoff = 0; For (gluint y = 0; y 

Note that XIB is used, so both imageview and scrollview are associated.

Attach a source image you have taken to enjoy it!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.