Processing images in iOS UIImage believe that when you do a project, you will certainly be useful to UIImage this class, so let's look at what's in this class. In fact, this article is to look at the document when you want to record the method in the document. UIImage inherit from NSObject the following describes the UIImage method in the first is our most commonly used by the image of the file name to get this image + (UIImage *) imagenamed: (NSString *) name// Note that this method applies to images that have been imported into the project to create a new picture 1, + (UIImage *) Imagewithcontentsoffile: (NSString *) path//file content under the specified path via a file to get new picture 2, + ( UIImage *) Imagewithdata: (NSData *) data//through a NSData object to get the picture 3, + (UIImage *) Imagewithdata: (NSData *) Data scale: (cgfloat) scale//Ibid, just plus a picture size ratio, used to change the size of the picture 4, + (UIImage *) Imagewithcgimage: (cgimageref) cgimage//use Quartz 2D object to create UIImage5, + ( UIImage *) Imagewithcgimage: (cgimageref) Imageref scale: (cgfloat) scale orientation: (uiimageorientation) orientation/ /Set the proportions and orientation of the picture, where the direction is an enumeration class. 6, + (UIImage *) Imagewithciimage: (Ciimage *) ciimage//create images with a core Image Object 7, + (UIImage *) Imagewithciimage: (Ciimage *) Ciimage Scales: (cgfloat) scale orientation: (uiimageorientation) orientation//plus proportions and picture Direction 8,-(UIImage *) Imagewithalignmentrectinsets: (uiedgeinsets) alignmentinsets//returns the image within the specified rectangular area 9, + (UIImage *) animatedimagenamed: ( NSString *)Name Duration: (nstimeinterval) duration//Create a dynamic picture, the duration of the dynamic picture is Duration10, + (UIImage *) Animatedimagewithimages: ( Nsarray *) Images Duration: (nstimeinterval) duration//create a dynamic picture with a set of images, dynamic duration duration11, + (UIImage *) Animatedresizableimagenamed: (NSString *) name Capinsets: (uiedgeinsets) capinsets Duration: (nstimeinterval) duration/ /create a dynamic picture on a variable-sized picture that specifies a rectangular area 12, + (UIImage *) animatedresizableimagenamed: (NSString *) name Capinsets: (uiedgeinsets) Capinsets Resizingmode: (uiimageresizingmode) Resizingmode Duration: (nstimeinterval) duration//Ibid, just a picture of a change in the way, Specifically, tiling or stretching 13,-(UIImage *) Resizableimagewithcapinsets: (uiedgeinsets) capinsets//Creating an image with a rectangular area 14,-(UIImage *) Resizableimagewithcapinsets: (uiedgeinsets) capinsets Resizingmode: (uiimageresizingmode) resizingmode//ibid., Specifies how the image changes to initialize the image method in some of the above methods can be found in the prototype, here is not one by one annotated 1, –initwithcontentsoffile://Load image from a file 2, –initwithdata:// NSData Object Initialization Picture 3, –initwithdata:scale://With NSData object, specified scale, initialize picture 4, –initwithcgimage:5, –initwithcgimage:scale:o Rientation:6, –iniTwithciimage:7, –initwithciimage:scale:orientation: Painting pictures 1, –drawatpoint://at the specified point to begin painting pictures, this point is the image of the top corner of the vertex 2,-(void) Drawatpoint: (cgpoint) point blendmode: (cgblendmode) Blendmode Alpha: (cgfloat) alpha//draws the entire picture at the specified points and uses the custom picture comp mode, and set the transparency 3, –drawinrect://in the specified area to draw the picture, you can scale the picture as necessary 4, –drawinrect:blendmode:alpha://reference above the second 5, –drawaspatterninrect:// In the specified range, tile the properties of the image Image Imageorientation//The direction of the picture size//Picture The scale resizingmode//Picture Change mode Cgimage Potential quartz image Ciimage//potential core image images//Returns an array of images, for a dynamic picture generated by a set of pictures duration//Returns the duration of the animated picture (that is, the animated picture is played again The time) Capinsets//Picture on the selected area Alignmentrectinsets//Picture tiled area iOS comes with an API such as the following [HTML] view plaincopy NSData *uiimagejpegrepr Esentation (UIImage *image, CGFloat compressionquality); There are two simple ways to read image data on the iphone: UIImageJPEGRepresentation and Uiimagepngrepresentation. The UIImageJPEGRepresentation function requires two parameters: a reference to the picture and a compression factor. Instead, uiimagepngrepresentation only requires a picture reference as a parameter. Through the actual use of the process, the comparison found: Uiimagepngrepresentation (uiimage* image) is more than UIImageJPEGRepresentation (UIImage* Image, 1.0) The amount of image data returned is much larger. For example, the same picture that reads the same view taken by the camera, Uiimagepngrepresentation () returns a size of 199K, and UIImageJPEGRepresentation (uiimage* image, 1.0) returns a data volume of only 140KB, which is less than 50 KB. If the clarity of the picture is not required, you can also set the The second parameter of the UIImageJPEGRepresentation function significantly reduces the amount of image data. For example, a picture just taken, when reading data by calling UIImageJPEGRepresentation (uiimage* image, 1.0), The returned data size is 140KB, but after changing the compression factor, when reading the data by calling UIImageJPEGRepresentation (uiimage* image, 0.5), the data returned is only 11KB large, greatly compressing the amount of data in the image And from a perspective point of view, the quality of the picture is not significantly reduced. Therefore, when reading the image data content, it is recommended to use uiimagejpegrepresentation, and according to their actual use of the scene, set the compression coefficient, further reduce the size of the image data. [HTML] View plaincopy UIImage *imagenew = [info objectforkey:@ "Uiimagepickercontrolleroriginalimage"]; Imagenew = [self imagewithimage:imagenew scaledtosize:cgsizemake (100, 100)]; NSData *imagedata = uiimagejpegrepresentation (imagenew, 0.0001); M_selectimage = [UIImage imagewithdata:imagedata]; . h specific code [HTML] View plaincopy #import <Foundation/Foundation.h> @interface UIImage (uiimageext)-(UIImage *) Scaletosize: (UIImage *) img Size: (cgsize) size; -(UIImage *) imagEbyscalingandcroppingforsize: (cgsize) targetsize; @end. M specific code[html] View plaincopy #import "UIImageExt.h" @implementation UIImage (Uiimageext)-(UIImage *) scale Tosize: (UIImage *) img Size: (cgsize) size{//Create a bitmap context//and set it as the currently in use context Uigraphicsbeginimag Econtext (size); Draw a picture of the size Change [img drawinrect:cgrectmake (0, 0, Size.width, size.height)]; Creates a changed-size picture from the current context uiimage* Scaledimage = Uigraphicsgetimagefromcurrentimagecontext (); Make the current context out of the stack uigraphicsendimagecontext (); Returns the new size of the image after the return scaledimage; }-(uiimage*) Imagebyscalingandcroppingforsize: (cgsize) targetsize {UIImage *sourceimage = self; UIImage *newimage = nil; Cgsize imageSize = sourceimage.size; CGFloat width = imagesize.width; CGFloat height = imagesize.height; CGFloat targetwidth = targetsize.width; CGFloat targetheight = targetsize.height; CGFloat scalefactor = 0.0; CGFloat sCaledwidth = Targetwidth; CGFloat scaledheight = targetheight; Cgpoint thumbnailpoint = Cgpointmake (0.0,0.0); if (Cgsizeequaltosize (imageSize, targetsize) = = NO) {cgfloat widthfactor = targetwidth/width; CGFloat heightfactor = targetheight/height; if (Widthfactor > Heightfactor) scalefactor = Widthfactor; Scale to fit height else scalefactor = heightfactor; Scale to fit width scaledwidth = width * scalefactor; Scaledheight = height * scalefactor; Center the image if (Widthfactor > Heightfactor) {thumbnailpoint.y = (Targetheight- scaledheight) * 0.5; } else if (Widthfactor < heightfactor) {Thumbnailpoint.x = (target width-scaledwidth) * 0.5; }} uigraphicsbeginimagecontext (Targetsize); This would crop CGRect thumbnailrect = Cgrectzero; Thumbnailrect.origin = Thumbnailpoint; ThumbnailRect.size.width = Scaledwidth; ThumbnailRect.size.height = Scaledheight; [Sourceimage Drawinrect:thumbnailrect]; NewImage = Uigraphicsgetimagefromcurrentimagecontext (); if (NewImage = nil) NSLog (@ "Could not scale image"); Pop the context to get back to the default Uigraphicsendimagecontext (); return newimage; } @end
IOS UIImage class Method summary