CoreGraphics相关方法

时间:2023-03-09 14:32:40
CoreGraphics相关方法

// 将view转为image(不经常用到的功能)(摘自SCCatWaitingHUD)

- (UIImage *)convertViewToImage

{

  CGSize s = self.bounds.size;

//下面方法,第一个参数表示区域大小。

//第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。

//第三个参数就是屏幕密度。

  UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);

  [self.layer renderIntoContext:UIGraphicsGetCurrentContext()];

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return image;

}

//***************************************

//切出一张图的一部分

- (UIImage *)getPartOfImage:(UIImage *)image rect:(CGRect)partRect
{
CGImageRef imageRef = image.CGImage;
CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, partRect);
UIImage *retImage = [UIImage imageWithCGImage:imagePartRef];
CGImageRelease(imagePartRef);
return retImage;
}

(未完待续)