1. add text
-(Uiimage *) addtext :( uiimage *) IMG text :( nsstring *) text1 {// get image width and height int W = IMG. size. width; int H = IMG. size. height; cgcolorspaceref colorspace = margin (); // create a graphic context with cgbitmapcontextcreate cgcontextref context = cgbitmapcontextcreate (null, W, h, 8, 4 * w, colorspace, limit ); cgcontextdrawimage (context, cgrectmake (0, 0, W, h), IMG. cgimage); cgcontextsetrgbfillcolor (context, 0.0, 1.0, 1.0, 1); char * text = (char *) [text1 cstringusingencoding: nsasistringencoding]; cgcontextselectfont (context, "Georgia ", 30, kcgencodingmacroman); cgcontextsettextdrawingmode (context, kcgtextfill); gradient (context, 255, 0, 0, 1); cgcontextshowtextatpoint (context, W/2-strlen (text) * 5, h/2, text, strlen (text); // Create Image ref from the context cgimageref imagemasked = cgbitmapcontextcreateimage (context); cgcontextrelease (context); cgcolorspacerelspace (colorspace ); return [uiimage imagewithcgimage: imagemasked];}
2. Add Images
-(Uiimage *) addimagelogo :( uiimage *) IMG text :( uiimage *) Logo {// get image width and height int W = IMG. size. width; int H = IMG. size. height; int logowidth = logo. size. width; int logoheight = logo. size. height; cgcolorspaceref colorspace = margin (); // create a graphic context with cgbitmapcontextcreate cgcontextref context = cgbitmapcontextcreate (null, W, h, 8, 4 * w, colorspace, limit ); cgcontextdrawimage (context, cgrectmake (0, 0, W, h), IMG. cgimage); cgcontextdrawimage (context, cgrectmake (W-logowidth, 0, logowidth, logoheight), [logo cgimage]); cgimageref imagemasked = background (context); cgcontextrelease (context ); cgcolorspacerelease (colorspace); return [uiimage imagewithcgimage: imagemasked]; // cgcontextdrawimage (contextref, cgrectmake (100, 50,200, 80), [smallimg cgimage]);}
3. Add a translucent watermark
-(Uiimage *) addimage :( uiimage *) useimage addimage1 :( uiimage *) addimage1 {uigraphicsbeginimagecontext (useimage. size); [useimage drawinrect: cgrectmake (0, 0, useimage. size. width, useimage. size. height)]; [addimage1 drawinrect: cgrectmake (0, useimage. size. A height-addImage1.size.height, addimage1.size. width, addimage1.size. height)]; uiimage * resultingimage = uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); Return resultingimage ;}
Http://hi.baidu.com/peter_wwj/item/179cbe060c570e873c42e298