1. Image Scaling: Code -(Uiimage *) resizeimage :( uiimage *) image towidth :( nsinteger) width height :( nsinteger) height {// create a graphics context with the target size // on iOS 4 and later, use uigraphicsbeginimagecontextwittions to take the scale into consideration // on iOS prior to 4, fall back to use uigraphicsbeginimagecontext cgsize size = cgsizemake (width, height); If (null! = Optional) uigraphicsbeginimagecontextwitexceptions (size, no, 0); else uigraphicsbeginimagecontext (size); cgcontextref context = uigraphicsgetcurrentcontext (); // flip the context because uikit coordinate system is upside down to quartz coordinate system cgcontexttranslatectm (context, 0.0, height); cgcontextscalectm (context, 1.0,-1.0 ); // draw the original image to Context cgcontextsetblendmode (context, kcgblendmodecopy); cgcontextdrawimage (context, cgrectmake (0.0, 0.0, width, height), image. cgimage); // retrieve the uiimage from the current context uiimage * imageout = uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); Return imageout;} 2. uiview rotation: code-(void) rotateview :( uiview *) view toangle :( float) angle {[uiview beginanimations: Nil Co Ntext: Nil]; [view settransform: cgaffinetransformmakerotation (angle)]; [uiview commitanimations];} 3. color processing function: Code # pragma mark color convert-(uicolor *) str2color :( nsstring *) STR {sel blacksel = nsselectorfromstring (STR); // nsselectorfromstring (@ "blackcolor "); uicolor * tcolor = nil; If ([uicolor respondstoselector: blacksel]) tcolor = [uicolor performselector: blacksel]; return tcolor;}-(uicolor *) hex Str2color :( nsstring *) hexstr {nsstring * cstring = [[hexstr stringbytrimmingcharactersinset: [nscharacterset whitespaceandnewlinecharacterset] uppercasestring]; // string shoshould be 6 or 8 characters if ([cstring length] <6) return [uicolor blackcolor]; // strip 0x if it appears if ([cstring hasprefix: @ "0x"]) cstring = [cstring substringfromindex: 2]; If ([cstring length]! = 6) return [uicolor blackcolor]; // separate into R, G, B substrings nsange range; range. location = 0; range. length = 2; nsstring * rstring = [cstring substringwithrange: range]; range. location = 2; nsstring * gstring = [cstring substringwithrange: range]; range. location = 4; nsstring * bstring = [cstring substringwithrange: range]; // scan values unsigned int R, G, B; [[ns1_scannerwithstring: RST Ring] scanhexint: & R]; [[ns1_scannerwithstring: gstring] scanhexint: & G]; [ns1_scannerwithstring: bstring] scanhexint: & B]; return [uicolor colorwithred :( (float) r/255.0f) Green :( (float) g/255.0f) Blue :( (float) B/255.0f) ALPHA: 1.0f];} -(nsstring *) color2str :( uicolor *) color {cgcolorref c = color. cgcolor; const cgfloat * components = cgcolorgetcomponents (c); size_t numberofcomponents = Cgcolorgetnumberofcomponents (c); nsmutablestring * s = [[nsmutablestring alloc] init] autorelease]; [s appendstring: @ "{"]; for (size_t I = 0; I <numberofcomponents; ++ I) {if (I> 0) {[s appendstring: @ ","];} [s appendstring: [nsstring stringwithformat: @ "% d", (INT) (components [I] * 255)];} [s appendstring: @ "}"]; return s ;}- (nsstring *) color2str1 :( uicolor *) color {const cgfloat * components = Cgcolorgetcomponents (color. cgcolor); nsstring * colorasstring = [nsstring stringwithformat: @ "% 1f, % 1f, % 1f, % 1f", components [0] * 255, components [1] * 255, components [2] * 255, components [3];} 4. the angle corresponding to the intermediate point of the Three-Point coordinate, and determine whether the code is clockwise-(bool) chkclockwise :( cgpoint) a B :( cgpoint) B C :( cgpoint) c {double AX =. x-B. x; double ay =-. Y + B. y; double Cx = C. x-B. x; double Cy =-C. Y + B. y; nslog (@ "Result: % d", (CY * Ax) <= (Ay * CX); nslog (@ "(x1, Y1) = (% 1f, % 1f)", ax, ay); nslog (@ "(X2, Y2) = (% 1f, % 1f) \ r \ n ", CX, CY); Return (CY * Ax) <= (Ay * CX ));} 5. after the coordinates are rotated, the new coordinates are obtained: code-(cgpoint) getnewpoint: (cgpoint) Old rotate :( double) rotate {// The actual coordinates are converted to the origin coordinate old. X = (old. x-center.x); old. y = (center. y-old.y); // calculate the new coordinate after selection double X = old. x * Cos (rotate)-old. y * sin (rotate); Double Y = old. y * Cos (rotate) + Old. x * sin (rotate); // The origin coordinate is converted to the actual coordinate X = center. X + X; Y = center. y-y; return cgpointmake (x, y);} 6. capture the color of any point in uiview: code-(uicolor *) getpixelcoloratlocation :( cgpoint) point {uigraphicsbeginimagecontext (self. bounds. size); [self. layer renderincontext: uigraphicsgetcurrentcontext ()]; uiimage * viewimage = uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); cgimageref inimage = viewimage. cgimage; uicolor * color = nil; // create off SC Reen bitmap context to draw the image. format argb is 4 bytes for each pixel: Alpa, red, green, blue cgcontextref cgctx = [self createargbbitmapcontextfromimage: inimage]; If (cgctx = NULL) {return nil; /* Error */} size_t W = cgimagegetwidth (inimage); size_t H = cgimagegetheight (inimage); cgrect rect = {0, 0}, {W, h }}; // draw the image to the bitmap context. once we draw, the memory // allo Cated for the context for rendering will then contain the // raw image data in the specified color space. cgcontextdrawimage (cgctx, rect, inimage); // now we can get a pointer to the image data associated with the bitmap // context. unsigned char * Data = cgbitmapcontextgetdata (cgctx); If (Data! = NULL) {// offset locates the pixel in the data from X, Y. // 4 for 4 bytes of data per pixel, W is width of one row of data. int offset = 4 * (w * Round (point. y) + round (point. x); int alpha = data [offset]; int Red = data [Offset + 1]; int Green = data [Offset + 2]; int Blue = data [Offset + 3]; nslog (@ "offset: % I colors: RGB a % I", offset, red, green, blue, alpha); color = [uicolor colorwithred :( red/255.0f) Green :( green/255.0f) Blue :( blue/255.0f) Alpha :( alpha/255.0f)];} // when finished, release the context cgcontextrelease (cgctx ); // free image data memory for the context if (data) {free (data);} return color;}-(cgcontextref) createargbbitmapcontextfromimage :( cgimageref) inimage {cgcontextref context = NULL; cgcolorspaceref colorspace; void * bitmapdata; int bitmapbytecount; int bitmapbytesper Row; // get image width, height. we'll use the entire image. size_t pixelswide = cgimagegetwidth (inimage); size_t pixelshigh = cgimagegetheight (inimage); // declare the number of bytes per row. each pixel in the bitmap in this // example is represented by 4 bytes; 8 bits each of red, green, blue, and // Alpha. bitmapbytesperrow = (pixelswide * 4); bitmapbytecount = (bitmapbytesperrow * pixelshigh); // Use the generic RGB color space. colorspace = cgcolorspacecreatedevicergb (); If (colorspace = NULL) {fprintf (stderr, "error allocating Color Space \ n"); return NULL;} // allocate memory for image data. this is the destination in memory // Where any drawing to the bitmap context will be rendered. bitmapdata = malloc (bitmapbytecount); If (bitmapdata = NULL) {fprintf (stderr, "memory not Located! "); Cgcolorspacerelease (colorspace); return NULL;} // create the bitmap context. we want pre-Multiplied argb, 8-bits // per component. regardless of what the source image format is // (CMYK, grayscale, and so on) It will be converted over to the format // specified here by cgbitmapcontextcreate. context = cgbitmapcontextcreate (bitmapdata, pixelswide, pixelshigh, 8, // bits per component bitmapb Ytesperrow, colorspace, kcgimagealphapremultipliedfirst); If (context = NULL) {free (bitmapdata); fprintf (stderr, "context not created! ") ;}// Make sure and release colorspace before returning cgcolorspacerel.pdf (colorspace); Return context ;}7. On the iPhone Program // Import the header file # import <quartzcore/quartzcore. h> // set the entire self. create an image imageuigraphicsbeginimagecontext (self. view. bounds. size); [self. view. layer renderincontext: uigraphicsgetcurrentcontext ()]; uiimage * image = encrypt (); uigraphicsendimagecontext (); // Save the image to the image uiimagewritetosavedphotosalbum (image, self, nil, nil); 8. draw a dot image:-(uiimage *) createimage {cgsize size = [[uiscreen mainscreen] bounds]. size; cgrect rect = cgrectmake (size. width/2, size. height/2, 5, 5); uigraphicsbeginimagecontext (size); cgcontextref context = uigraphicsgetcurrentcontext (); unsigned int red, green, blue; // fill color. cgcontextsetrgbfillcolor (context, 0, 0.7, 0.7, 1.0); // your drawing code. cgcontextfillellipseinrect (context, rect); // get the image and return. uiimage * image = uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); Return image ;}
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.