////uiimage+uitool.h//Picture Clipping////Created by Yaguangzhu on 15/9/10.//Copyright (c) 2015 Yaguangzhu. All rights reserved.//#import<UIKit/UIKit.h>@interfaceUIImage (Uitool)+ (Instancetype) Imagewithname: (NSString *) name border: (cgfloat) Border bordercolor: (Uicolor *) color;@end////UIIMAGE+UITOOL.M//Picture Clipping////Created by Yaguangzhu on 15/9/10.//Copyright (c) 2015 Yaguangzhu. All rights reserved.//#import "uiimage+uitool.h"@implementationUIImage (Uitool)+ (Instancetype) Imagewithname: (NSString *) name border: (cgfloat) Border bordercolor: (Uicolor *) color{cgfloat borderw=border; UIImage*oldimage =[UIImage Imagenamed:name]; CGFloat Imagew= OldImage.size.width +2*Borderw; CGFloat Imageh= oldimage.size.height+2*Borderw; CGFloat CIRCIRW= Imagew > Imageh?Imageh:imagew; Uigraphicsbeginimagecontextwithoptions (Cgsizemake (CIRCIRW, CIRCIRW), NO,0.0); Uibezierpath*path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, CIRCIRW, CIRCIRW)]; Cgcontextref CTX=Uigraphicsgetcurrentcontext (); Cgcontextaddpath (CTX, path.) Cgpath); [ColorSet]; Cgcontextfillpath (CTX); CGRect CLIPR=CGRectMake (Borderw, Borderw, OldImage.size.width, oldImage.size.height); Uibezierpath*clippath =[Uibezierpath BEZIERPATHWITHOVALINRECT:CLIPR]; [Clippath Addclip]; [Oldimage Drawatpoint:cgpointmake (Borderw, BORDERW)]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnnewimage;}@end
////VIEWCONTROLLER.M//Picture Clipping////Created by Yaguangzhu on 15/9/10.//Copyright (c) 2015 Yaguangzhu. All rights reserved.//#import "ViewController.h"#import "uiimage+uitool.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*ImageView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //White Ring Edition_imageview.image = [UIImage imagewithname:@"001"BorderTenBordercolor:[uicolor Redcolor]]; }- (void) clipcrile{cgfloat borderw=5; UIImage*oldimage = [UIImage imagenamed:@"001"]; CGFloat Imagew= OldImage.size.width +2*Borderw; CGFloat Imageh= oldimage.size.height+2*Borderw; CGFloat CIRCIRW= Imagew > Imageh?Imageh:imagew; Uigraphicsbeginimagecontextwithoptions (Cgsizemake (CIRCIRW, CIRCIRW), NO,0.0); Uibezierpath*path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, CIRCIRW, CIRCIRW)]; Cgcontextref CTX=Uigraphicsgetcurrentcontext (); Cgcontextaddpath (CTX, path.) Cgpath); [[Uicolor Whitecolor]Set]; Cgcontextfillpath (CTX); CGRect CLIPR=CGRectMake (Borderw, Borderw, OldImage.size.width, oldImage.size.height); Uibezierpath*clippath =[Uibezierpath BEZIERPATHWITHOVALINRECT:CLIPR]; [Clippath Addclip]; [Oldimage Drawatpoint:cgpointmake (Borderw, BORDERW)]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); _imageview.image=newimage;}- (void) clip{UIImage*oldimage = [UIImage imagenamed:@"001"]; Uigraphicsbeginimagecontextwithoptions (Oldimage.size, NO,0.0); Uibezierpath*path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, OldImage.size.width, OldImage.size.height)]; [Path Addclip]; [Oldimage Drawatpoint:cgpointzero]; UIImage*newimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); _imageview.image=newimage;}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
ios-cropping and clipping strokes plus clipping into a method class