//1.加載原圖 UIImage *oldImage = [UIImage imageNamed:@"girl"]; //2.開啓上下文 UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0); //3.取得當前的上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //4.畫圓 CGRect circleRect = CGRectMake(0, 0, oldImage.size.width, oldImage.size.height); CGContextAddEllipseInRect(ctx, circleRect); //5.裁剪(按照當前的路徑形狀裁剪) CGContextClip(ctx); //6.畫圖 [oldImage drawInRect:circleRect]; //7.取圖 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); //8.結束 UIGraphicsEndImageContext(); //9.寫出文件 NSData *data = UIImagePNGRepresentation(newImage); NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *realpath = [path stringByAppendingPathComponent:@"new.png"]; [data writeToFile:realpath atomically:YES]; //打印一下圖片的沙盒路徑 NSLog(@"%@",realpath);