一、图片截取
要从一个图片UIImage中截取自己想要的图片,可以使用以下方法:
参数
1、第一个参数是原图image
2、第二个参数是,想要截取的区域
//从原图取指定大小的图片(uiimage)
-(UIImage*)longImageScreenWithImage:(UIImage*)sourceImage withRect:(CGRect)myImageRect{
CGImageRef imageRef = sourceImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
//转换img
UIImage *image = [UIImageimageWithCGImage:subImageRef];
CGImageRelease(subImageRef);//release
return image;
}
步骤分析1、CGImageRef imageRef = sourceImage.CGImage;
从UIImage转换为CGImage,CGImageRef是一个C语言的结构体:
typedef struct CF_BRIDGED_TYPE(id) CGImage *CGImageRef;
注:看到Ref相关的类型,其实都是C语言结构体定义的,作用与对象的指针相同,是iOS中C相关的指针类型。2、CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGImageCreateWithImageInRect函数
官方解释:Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
意为:从一个已存在的bitmap image中的子区域,获取并创建一个bitmap image。
bitmap意思是位图,是苹果内部处理图片的方式。
第一个参数是父图片,第二个参数是要获取的区域,返回得到的CGImage,再把他转换为UIimage。
3、CGImageRelease(subImageRef);
释放中间变量subImageRef,否则会有内存泄露。
未完待续。。
本文介绍了一种在iOS中从UIImage中裁剪指定区域的方法,包括将UIImage转换为CGImage,使用CGImageCreateWithImageInRect函数从源图像中提取指定区域,并将其转换回UIImage的过程。
127

被折叠的 条评论
为什么被折叠?



