一
UIImage *image =[UIImage imageNamed:@"tt"];
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
UIBezierPath *path =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[path addClip];
[image drawAtPoint:CGPointZero];
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_imageView =[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-50, self.view.frame.size.height/2-50, 100, 100)];
_imageView.image=clipImage;
[self.view addSubview:_imageView]
二
-(UIImage*)cutImg:(UIImage*)image withSize:(CGSize)asize{
UIImage *newimage;
if (nil == image) {
newimage = nil;
} else {
CGSize oldsize = image.size;
CGRect rect;
if ((asize.width / asize.height) > (oldsize.width / oldsize.height)) {
rect.size.width = asize.width;
rect.size.height = (asize.width * oldsize.height) / oldsize.width;
rect.origin.x = 0;
rect.origin.y = (asize.height - rect.size.height) / 2;
} else {
rect.size.width = (asize.height * oldsize.width) / oldsize.height;
rect.size.height = asize.height;
rect.origin.x = (asize.width - rect.size.width) / 2;
rect.origin.y = 0;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, asize.width, asize.height));
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, asize.width, asize.height));
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}