#import <UIKit/UIKit.h> @interface Uicolor (randomcolor) + (Uicolor *) Randomcolor; @end #import "uicolor+ RandomColor.h "@implementation Uicolor (Randomcolor) + (Uicolor *) randomcolor{cgfloat hue = (arc4random ()% 256/256.0 ); 0.0 to 1.0 cgfloat saturation = (arc4random ()% 128/256.0) + 0.5; 0.5 to 1.0,away from white cgfloat brightness = (arc4random ()% 128/256.0) + 0.5; 0.5 to 1.0,away from black return [Uicolor Colorwithhue:hue saturation:saturation brightness:brightness alpha:1]; @end draw randomly generated colors into a picture. -(void) createimage{nsstring* path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask,yes ) objectatindex:0]; Path = @ "/users/shouqiangwei/desktop/unnamed folder 2"; NSLog (@ "path =%@", path); NSString *imgname; NSString *imgurl; NSString *marknum; UIImage *img; NSData *imgdata; Nserror *err; int width=, height=1000, fontSize; Cgpoint Markpoint; int baselength = 320; Nsfilemanager *FM = [NsfilemAnagerdefaultmanager]; for (int i=0; i<1000; i++) {Uicolor *color = [Uicolorrandomcolor]; [_currentcolorimageviewsetbackgroundcolor:color]; Imgname = [NSString stringwithformat:@ "%i.jpg", i+1]; _lblcount.text = imgname; Imgurl = [path stringbyappendingpathcomponent:imgname]; if (![ FM Fileexistsatpath:imgurl]) {Const CGFloat *colorcomps = cgcolorgetcomponents (color. Cgcolor); Marknum = [NSString stringwithformat:@ "%i", i+1]; FontSize = WIDTH/5; Markpoint = Cgpointmake (FONTSIZE/2, FONTSIZE/2); Uigraphicsbeginimagecontext (Cgsizemake (width, height)); Cgcontextref CTX =uigraphicsgetcurrentcontext (); Cgcontextsetrgbfillcolor (CTX, colorcomps[0], colorcomps[1], colorcomps[2], colorcomps[3]); Cgcontextfillrect (CTX, cgrectmake (0, 0, width, height)); Cgcontextsetrgbfillcolor (CTX, 1.0, 1.0, 1.0,1.0); Create a number on the picture. [Marknum DRAWATPOint:markpoint Withfont:[uifont Systemfontofsize:fontsize]]; img = Uigraphicsgetimagefromcurrentimagecontext (); self.imageview.image= img; Uigraphicsendimagecontext (); Create to Local. Imgdata = [Nsdatadatawithdata:uiimagejpegrepresentation (img,1.0f)]; [Imgdata writetofile:imgurloptions:nsdatawritingatomicerror:&err]; if (err) {NSLog (@ "error:%@\nimage:%@", Err.localizeddescription, imgname); } else {NSLog (@ "Done:%@", imgname); } } }}