对图片进行分割 ---- 抠图

//<4> 创建16个UIImageView显示分割以后的小图片
for(int i = 0;i<4;i++)
{
for(int j = 0;j<4;j++)
{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(2 + width * j, 2 + height * i, width - 2, height -2)];
//imageView.backgroundColor = [UIColor orangeColor];

//<5>对图片进行分割 ---- 抠图
//图片分割类CGImageRef
//CGImageRef这个类会根据提供的视图的大小进行等比例分割
//CGImageCreateWithImageInRect
//第一个参数:被分割的图片的CGImage类型
//第二个参数:将图片按照什么尺寸划分

CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, imageView.frame);

//image.CGImage 这个方法是将UIImage转化成CGImage

//<6>将CGImage类型的图片转化成UIImage类型的图片
UIImage * littleImage = [UIImage imageWithCGImage:imageRef];

//imageWithCGImage:这个方法是将CGImage转化成UIImage

//<7>将16个image添加到16个UIImageView视图上
imageView.image = littleImage;

//<8>隐藏一个小的子视图
if(i * j == 9)
{
imageView.tag = 998;
[imageView setHidden:YES];
}

//<9>为每一个小的子视图添加手势
imageView.userInteractionEnabled = YES;
//调用自定义方法
[self addGestureRecgnizerOnImageView:imageView];

[backView addSubview:imageView];

}
}

}

群号:186052819
原文地址:https://www.cnblogs.com/zuidap/p/5788628.html