Uigraphicsbeginimagecontextwithoptions, this article mainly in the picture type context to manipulate the picture, the concrete realization function:
- 1.生成图片
- 2.绘制图片到视图
- 3.添加水印
- 4.截取屏幕或者相应view
- 5.图片擦除
- 6.图片裁剪
Specific methods used in the introduction of the method is explained, in order to reuse the code, the above method is encapsulated, placed in the UIImage category, convenient for future use.
Basic steps for picture manipulation
1 . Turn on the graphics context 2 . Draw pictures - use Drowinrect or drawatpoint to draw pictures (where is the difference?). You can think about it first. Drawinrect is the area that is drawn with Rect as a picture, and the picture is drawn in the size of the current region's image in a fill, and the width-to-height ratio of the rect and the width-to-height ratio of the original picture do not distort the picture at the same time. Drowatpoint is the starting point for a picture drawing, the size of the picture is still the size of the original picture, does not deform the picture - The layer is rendered in the current context 3. Get a new picture from the current context 4. Close the context
1. Create a picture, here we generate a specific color of the picture
+ (UIImage *) Createimagecolor: (Uicolor *) Color size: (cgsize) Size {//turn on the graphics contextUigraphicsbeginimagecontextwithoptions (Size, NO,0); //Draw a color areaUibezierpath *path = [Uibezierpath bezierpathwithrect:cgrectmake (0,0, Size.width, Size.Height)]; [Color Setfill]; [path fill];//Cgcontextref CTX = Uigraphicsgetcurrentcontext ();//cgcontextsetfillcolorwithcolor (CTX, color. Cgcolor);//Cgcontextfillrect (CTX, cgrectmake (0, 0, size.width, size.height)); //get a picture from a graphics contextUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); //Close the graphics contextUigraphicsendimagecontext (); returnNewImage;}
2. Draw the picture, you can set the picture scale or specify the size of the compressed picture, can be used as a compressed image
+ (UIImage *) Scaleimage: (UIImage *) Image sclae: (cgfloat) Scale {//determine the size after compressionCGFloat scalewidth = Image.size.width *Scale ; CGFloat ScaleHeight= Image.size.height *Scale ; Cgsize scalesize=Cgsizemake (ScaleWidth, ScaleHeight); //turn on the graphics contextUigraphicsbeginimagecontext (scalesize); //Drawing Pictures[Image Drawinrect:cgrectmake (0,0, ScaleWidth, ScaleHeight)]; //get a picture from a graphics contextUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); //Close the graphics contextUigraphicsendimagecontext (); returnNewImage;}
3. Draw a watermark
Text watermark
Maybe after you add a text watermark, the text font is not the font size you set, because the size of the canvas is the size of the picture, and when the picture is added to the ImageView, it may be compressed or enlarged, and the text will change.
+ (UIImage *) Wateratimage: (UIImage *) Image text: (NSString*) Text point: (cgpoint) Point attributes: (Nsdictionary*) Attributes {//turn on the graphics contextUigraphicsbeginimagecontextwithoptions (Image.size, NO,0); //Drawing Pictures[Image Drawinrect:cgrectmake (0,0, Image.size.width, Image.size.height)]; //Add text[text Drawatpoint:point withattributes:attributes]; //Get PicturesUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); //Close ContextUigraphicsendimagecontext (); returnNewImage;}
Image watermark
+ (UIImage *) Wateratimage: (UIImage *) Image waterimgae: (UIImage*) waterimage rect: (cgrect) rect {//turn on the graphics contextUigraphicsbeginimagecontextwithoptions (Image.size, NO,0); //draw the original picture[Image Drawinrect:cgrectmake (0,0, Image.size.width, Image.size.width)]; //Draw a watermark[Waterimage Drawinrect:rect]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnNewImage;}
4. Screen Cutting
1 . Creating a Graphics context 2 . Render a layer of view to a graphics context 3 . Getting pictures from the graphics context 4. Close the image context
Of course if you just need a snapshot of a view, you can use [view Snapshotviewafterscreenupdates:no] After iOS7, for example, to achieve long press drag cell operation
+ (void) Cutview: (UIView *) View success: (void(^) (UIImage *image)) Success { // Open Graphics context 0); // gets the current context Cgcontextref CTX = uigraphicsgetcurrentcontext (); // Rendering [View.layer renderincontext:ctx]; *newimage = uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); Success (NewImage);}
5. Erase
1. Set up two pictures, above for the pictures we want to erase, and the rear for the pictures we need to show
2. Set the size and location of the erased area
+ (UIImage *) Wipeview: (UIView *View point: (cgpoint) point size: (cgsize) Size {//turn on the graphics contextUigraphicsbeginimagecontext (view.bounds.size); //gets the current contextCgcontextref CTX =Uigraphicsgetcurrentcontext (); //Rendering[View.layer Renderincontext:ctx]; //Calculate the Erase rectCGFloat clipx = point.x-size.width/2; CGFloat clipy= point.y-size.height/2; CGRect ClipRect=CGRectMake (clipx, clipy, Size.width, size.height); //Set the zone to transparentCgcontextclearrect (CTX, clipRect); //get a new pictureUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnNewImage;}
6. Picture clipping
Clipping of rectangular areas, where we use the Drowatpoint feature to crop
1. Suppose that the size of a picture is ( -, -)2The. ImageView size is ( -, -)3The cropping area we selected is (Ten,Ten, Max, Max)4In order to be able to maintain the resolution of the original image, the image is clipped on the original image without stretching, so the actual clipping area on the image is ( -, -, -, -)5The point at which the picture is drawn on the canvas is (- -,- -) so that we don't need to crop to get the picture we want to get
+ (UIImage *) Cutimage: (UIImage *) Image imageviewsize: (cgsize) size clipRect: (cgrect) rect {//ratio of picture size to actual display sizeCGFloat scale_width = image.size.width/Size.width; CGFloat scale_height= image.size.height/Size.Height; //Actual clipping areaCGRect clipRect = CGRectMake (rect.origin.x *Scale_width, Rect.origin.y*Scale_height, Rect.size.width*Scale_width, Rect.size.height*scale_height); //turn on the graphics contextUigraphicsbeginimagecontext (cliprect.size); //Drawing[Image Drawatpoint:cgpointmake (-cliprect.origin.x,-CLIPRECT.ORIGIN.Y)]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnnewimage;}Custom Point Lines
1 Convert a custom point to a point on a picture 2 . Calculates the region in which the custom point resides rect 3 . Start the graphics context, Rect.size 4 . Draw path, model trim 5. Drawing drawatpoint,x:-rect.origin.x y:- rect.origin.y6. Get the picture from the graphics context 7 . Close the graphics context
+ (UIImage *) Cutimage: (UIImage *) Image imageviewsize: (cgsize) size clippoints: (Nsarray*) Points {//ratio of picture size to actual display sizeCGFloat scale_width = image.size.width/Size.width; CGFloat scale_height= image.size.height/Size.Height; //to work with trimmed pointsNsarray *newpoints =[UIImage points:points scalex:scale_width scaley:scale_height]; //determine the point of the upper and lower left edge//x Ascending ArrayNsarray *point_x = [newpoints sortedarrayusingcomparator:^nscomparisonresult (Nsvalue *obj1, NSValue *obj2) {Cgpoint Point1=[Obj1 Cgpointvalue]; Cgpoint Point2=[Obj2 Cgpointvalue]; returnpoint1.x >point2.x; }]; //y Ascending ArrayNsarray *point_y = [newpoints sortedarrayusingcomparator:^nscomparisonresult (Nsvalue *obj1, NSValue *obj2) {Cgpoint Point1=[Obj1 Cgpointvalue]; Cgpoint Point2=[Obj2 Cgpointvalue]; returnPoint1.y >point2.y; }]; //determine the area of the cutCGRect ClipRect =CGRectMake ([Point_x.firstobject cgpointvalue].x, [Point_y.firstobject cgpointvalue].y , [Point_x.lastobject cgpointvalue].x-[Point_x.firstobject cgpointvalue].x, [Point_y.lastobject cgpointvalue].y-[Point_y.firstobject cgpointvalue].y); //turn on the graphics contextUigraphicsbeginimagecontext (cliprect.size); Uibezierpath*path =[Uibezierpath Bezierpath]; for(Nsinteger i =0; i < Newpoints.count; i + +) {Cgpoint point=[newpoints[i] cgpointvalue]; if(i = =0) {[path movetopoint:point]; } Else{[path addlinetopoint:point]; }} [path Closepath]; [Path Addclip]; //Drawing[Image Drawatpoint:cgpointmake (-cliprect.origin.x,-CLIPRECT.ORIGIN.Y)]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnNewImage;}
Original
No love
Links: http://www.jianshu.com/p/3baddf100b67
IOS drawing (some actions of uiimage)