IOS implements code that takes color from the background image _ios

Source: Internet
Author: User
Tags set background

This article explains the iOS from the background Image color code, shared for everyone to refer to, the specific contents are as follows

Implementation code:

void *bitmapdata;
 
A pointer to the memory space equal to the number of bytes used by the image to use the RGB channel.
  Static Cgcontextref Creatergbabitmapcontext (Cgimageref inimage) {cgcontextref context = NULL;
  Cgcolorspaceref ColorSpace;
  int bitmapbytecount;
 
  int bitmapbytesperrow; size_t pixelswide = Cgimagegetwidth (inimage);
 
  Gets the number of horizontal pixels size_t Pixelshigh = cgimagegetheight (inimage); Bitmapbytesperrow = (Pixelswide * 4); The number of bytes in each row is occupied by the pixel, and each pixel ARGB four channels each occupy 8 bit (0-255) of space Bitmapbytecount = (Bitmapbytesperrow * pixelshigh); Calculates the number of bytes occupied by an entire graph ColorSpace = Cgcolorspacecreatedevicergb ()//Create a device-dependent RGB channel/memory space allocated enough to hold the number of pictures bytes BitmapData = malloc (b
  Itmapbytecount);
                   Creates a coregraphic graphics context that describes some of the drawing parameters of the image that the bitmadata points to in the memory space need to be drawn in the context = Cgbitmapcontextcreate (BitmapData,
                   Pixelswide, Pixelshigh, 8, Bitmapbytesperrow,
  ColorSpace, Kcgimagealphapremultipliedlast); In the Core Foundation, a pointer created by the name of the method containing the Create, alloc, is required to use the CFRelease () function releases cgcolorspacerelease (colorspace);
return context; //Returns a pointer to an array in which each of the four elements in the array is the RGBA value of a pixel on the image (0-255), with unsigned char because its exact range of values is 0-255 static unsigned char *
  Requestimagepixeldata (UIImage *inimage) {cgimageref img = [Inimage cgimage];
  Cgsize size = [inimage size];
  Use the above function to create context cgcontextref CGCTX = Creatergbabitmapcontext (IMG);
  CGRect rect = {{0,0},{size.width, size.height}};
  Draws the target image to the specified context, which is actually the bitmapdata within the context.
  Cgcontextdrawimage (CGCTX, Rect, IMG);
  unsigned char *data = Cgbitmapcontextgetdata (CGCTX);
  Releases the context cgcontextrelease (CGCTX) created by the above function;
return data;
  //Set Background picture--(void) Setsourceimage: (NSString *) sourceimage imagewidth: (int) _width imageheight: (int) _height {
  Generates a background figure of the specified size uiimage *im = [UIImage imagenamed:sourceimage];
  UIImage *newimage;
  Uiimageview *view = [[Uiimageview alloc] initwithimage:im];
  View.frame = CGRectMake (0, 0, _width, _height); Uigraphicsbeginimagecontext (Cgsizemake (_width, _height)); Size is Cgsize classType, that is, the picture size you need [im Drawinrect:cgrectmake (0, 0, _width, _height)];
  NEWIMAGERECT Specifies the picture drawing area NewImage = Uigraphicsgetimagefromcurrentimagecontext ();
 
  Uigraphicsendimagecontext ();
  width = newImage.size.width;
  Height = newImage.size.height;
The analytic background image is pixel, and the Imgpixel = Requestimagepixeldata (newimage) is used for color extraction.  }//Calculation color-(uicolor*) Calcolor: (cgpoint) apoint {int i = 4 * Width * ROUND (APOINT.Y+IMAGEVIEW.FRAME.SIZE.HEIGHT/2) + 4 *
  Round (APOINT.X+IMAGEVIEW.FRAME.SIZE.WIDTH/2);
  int _r = (unsigned char) imgpixel[i];
  int _g = (unsigned char) imgpixel[i+1];
  int _b = (unsigned char) imgpixel[i+2];
  NSLog (@ "(%f,%f)", APOINT.X,APOINT.Y);
  NSLog (@ "Red:%f Green:%f Blue:%f", _r/255.0,_g/255.0,_b/255.0);
return [Uicolor colorwithred:_r/255.0f green:_g/255.0f blue:_b/255.0f alpha:1.0];
  }-(void) Changcolor: (Uicolor *) color{int width_; if (![
  Util Isipad]) {width_ = 30;
  else {width_ = 70;
  } uigraphicsbeginimagecontext (Cgsizemake (Width_, width_)); Cgcontextref Ctx = Uigraphicsgetcurrentcontext ();
  Cgcontextmovetopoint (CTX, 20, 20); Cgcontextsetfillcolorwithcolor (CTX, color.
  Cgcolor); if (![
  Util Isipad]) {Cgcontextaddarc (CTX, WIDTH_/2, WIDTH_/2, 14.5, 0, 6.3, 0);
  else {Cgcontextaddarc (CTX, width_/2+0.5, WIDTH_/2, 31.3, 0, 6.3, 0);
  } cgcontextfillpath (CTX);
  Self->pickedcolorimageview.image = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
 }

The above is the entire content of this article, I hope to help you learn.

Related Article

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.