UIImage image processing: screenshot, zoom, set size, storage

Source: Internet
Author: User

Image processing is approximate (capture), zoom (scale), set size (resize), storage (Save)
These are good to deal with, there are filters, wipe, etc.
In this demo code, I wrote a few ways


1. Equal ratio Scaling
-(UIImage *) Scaleimage: (UIImage *) image toscale: (float) scalesize

{

Uigraphicsbeginimagecontext (Cgsizemake (Image.size.width * scalesize, Image.size.height * scalesize);
[Image drawinrect:cgrectmake (0, 0, image.size.width * scalesize, Image.size.height * scalesize)];
UIImage *scaledimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();


return scaledimage;

}


2. Self-setting length and width
-(UIImage *) Resizeimage: (UIImage *) image tosize: (cgsize) reSize

{
Uigraphicsbeginimagecontext (Cgsizemake (Resize.width, resize.height));
[Image drawinrect:cgrectmake (0, 0, Resize.width, resize.height)];
UIImage *resizeimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();


return resizeimage;

}


3. Handling a specific view
As long as the object that inherits UIView can handle
You must first import quzrtzcore.framework


-(uiimage*) Captureview: (UIView *) TheView

{
CGRect rect = theview.frame;
Uigraphicsbeginimagecontext (rect.size);
Cgcontextref context = Uigraphicsgetcurrentcontext ();
[Theview.layer Renderincontext:context];
UIImage *img = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();


return img;

}


4. Storing images
Save the image here into the file stored in the app and store it in the picture library of your phone

1) stored in the app file
NSString *path = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]stringbyappendingpathcomponent:@ " Image.png "];
[Uiimagepngrepresentation (image) WriteToFile:pathatomically:YES];
This will take the picture you are dealing with and save it to the documents in the app home under the Image.png file name.

2) store in the picture library of your phone
Cgimageref screen = Uigetscreenimage ();
uiimage* image = [UIImage imagewithcgimage:screen];
Cgimagerelease (screen);
Uiimagewritetosavedphotosalbum (image, self, nil, nil);
Uigetscreenimage () was originally private (private) API, used to intercept the entire noodle
After SDK 4.0, Apple was released.

In addition, the library must be used in the virtual machine, and the simulator cannot be used


The following codes are used for the quartz framework and the core Graphics framework. Add these two frameworks to the workspace's framework catalog. In Uikit, images like UIImage and cgimageref are done through the graphics context. The Graphics context seals the variables and makes it easy to manipulate images in different sitting relationships. The lack of access to images is not so convenient. The noodles will give you the code to get the numbers.

Capture images from UIView as a screenshot of the window. iOS provides global full screen screenshot function Uigetscreenview (). If you need a specific area of the image, you can crop.

    1. Cgimageref screen = Uigetscreenimage ();
    2. uiimage* image = [UIImage imagewithcgimage:screen];

For a specific UIView screen, you can output the layer of the current view into a imagecontext, and then use this imagecontext to get uiimage

    1. -(uiimage*) Captureview: (UIView *) TheView
    2. {
    3. CGRect rect = theview.frame;
    4. Uigraphicsbeginimagecontext (rect.size);
    5. Cgcontextref context =uigraphicsgetcurrentcontext ();
    6. [Theview.layer Renderincontext:context];
    7. UIImage *img = Uigraphicsgetimagefromcurrentimagecontext ();
    8. Uigraphicsendimagecontext ();

    9. return img;
    10. }

If you need to crop the region, you can path & clip, the following example is to build a 200x200 image context, and then intercept the upper left corner

    1. Uigraphicsbeginimagecontext (Cgmakesize (200,200));
    2. Cgcontextrefcontext=uigraphicsgetcurrentcontext ();
    3. Uigraphicspushcontext (context);
    4. // ... Write the diagram to the context, omitting [Indent]cgcontextbeginpath ();
    5. Cgcontextaddrect (Cgmakerect (0,0,100,100));
    6. Cgcontextclosepath (); [/indent] Cgcontextdrawpath ();
    7. Cgcontextflush (); Enforce the actions defined above
    8. uiimage* image = Uigraphicgetimagefromcurrentimagecontext ();
    9. Uigraphicspopcontext ();

Stored images are stored into home directory files and picture library files. stored in a directory file is like this

    1. NSString *path = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Image.png "];
    2. [Uiimagepngrepresentation (image) Writetofile:path Atomically:yes];

To save to a picture library

    1. Uiimagewritetosavedphotosalbum (image, nil, nil, nil);


Uimage encapsulates Cgimage, which is easy to convert from one to the other

    1. Uiimage* Imui=nil;
    2. Cgimageref Imcg=nil;
    3. Imui = [UIImage initwithcgimage:imcg];
    4. IMCG = Imui.cgimage;

Get images from Cgimage, have QA on Apple Dev, and don't seem to support iOS
To give an example of a color inversion on iOS

  1. -(ID) Invertcontrast: (uiimage*) img
  2. {
  3. Cgimageref inimage = img. Cgimage;
  4. Cgcontextref CTX;
  5. Cfdataref M_dataref;
  6. M_dataref = Cgdataprovidercopydata (Cgimagegetdataprovider (inimage));

  7. int width = cgimagegetwidth (inimage);
  8. int height = cgimagegetheight (inimage);

  9. int BPC = cgimagegetbitspercomponent (inimage);
  10. int bpp = Cgimagegetbitsperpixel (inimage);
  11. int BPL = Cgimagegetbytesperrow (inimage);

  12. UInt8 * M_pixelbuf = (UInt8 *) cfdatagetbyteptr (M_DATAREF);
  13. int length = Cfdatagetlength (M_DATAREF);

  14. NSLog (@ "Len%d", length);
  15. NSLog (@ "width=%d, height=%d", width, height);
  16. NSLog (@ "1=%d, 2=%d, 3=%d", BPC, BPP,BPL);

  17. for (int index = 0; index < length; index + = 4)
  18. {
  19. M_pixelbuf[index + 0] = 255-m_pixelbuf[index + 0];//b
  20. M_pixelbuf[index + 1] = 255-m_pixelbuf[index + 1];//g
  21. M_pixelbuf[index + 2] = 255-m_pixelbuf[index + 2];//r
  22. }

  23. CTX = Cgbitmapcontextcreate (m_pixelbuf, width, height, BPB, BPL, Cgimagegetcolorspace (Inimage), Kcgimagealphapremultipliedfirst);
  24. Cgimageref imageref = Cgbitmapcontextcreateimage (CTX);
  25. uiimage* rawimage = [UIImage imagewithcgimage:imageref];
  26. Cgcontextrelease (CTX);
  27. return rawimage;
  28. }

It's easy to figure out an algorithm that looks like a map. The unsigned char* to graphics context, or uiimage or and cgimageref, to show images of the region.

      1. Cgcontextref CTX = cgbitmapcontextcreate (Pixelbuf,width,height, Bitspercomponent,bypesperline, ColorSpace, Kcgimagealphapremultipliedlast);
      2. Cgimageref imageref = Cgbitmapcontextcreateimage (CTX);
      3. uiimage* image = [UIImage imagewithcgimage:imageref];
      4. nsstring* path = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Ss.png "];
      5. [Uiimagepngrepresentation (Self.image) Writetofile:path Atomically:yes];
      6. Cgcontextrelease (CTX);

UIImage image processing:, scaling, setting size, storing

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.