IOS开发点滴---UIView转UIImage以及切割UIView
2015-07-14 12:21
375 查看
//view转UIImage
- (UIImage *)viewConvertToImage:(UIView *)view
{
//创建image的上下文环境
UIGraphicsBeginImageContext(view.bounds.size);
//将view的layer渲染到当前的上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//从当前上下文环境中获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文环境
UIGraphicsEndImageContext();
return image;
}
//切割UIView
- (NSArray *)getSubViewArrayFromeSuperView:(UIView *)superView
{
NSUInteger rowNum = 10;
NSUInteger colNum = 10;
NSInteger height = superView.frame.size.height/rowNum;
NSInteger width = superView.frame.size.width/colNum;
NSInteger baseX = superView.frame.origin.x;
NSInteger baseY = superView.frame.origin.y;
NSMutableArray *imageArr = [[NSMutableArray alloc] initWithCapacity:rowNum*colNum];
UIImage *superImage = [self viewConvertToImage:superView];
for (int i = 0; i < rowNum*colNum; i++) {
UIImageView *subImageView = [[UIImageView alloc] initWithFrame:CGRectMake(baseX + (i%colNum)*width, baseY+(i/rowNum)*height, width, height)];
//从UIImage中切出CGImage,再进行转化
CGImageRef cgImage = CGImageCreateWithImageInRect(superImage.CGImage, CGRectMake((i%colNum)*width, (i/rowNum)*height, width, height));
subImageView.image = [UIImage imageWithCGImage:cgImage];
[imageArr addObject:subImageView];
}
return imageArr;
}
- (UIImage *)viewConvertToImage:(UIView *)view
{
//创建image的上下文环境
UIGraphicsBeginImageContext(view.bounds.size);
//将view的layer渲染到当前的上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//从当前上下文环境中获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文环境
UIGraphicsEndImageContext();
return image;
}
//切割UIView
- (NSArray *)getSubViewArrayFromeSuperView:(UIView *)superView
{
NSUInteger rowNum = 10;
NSUInteger colNum = 10;
NSInteger height = superView.frame.size.height/rowNum;
NSInteger width = superView.frame.size.width/colNum;
NSInteger baseX = superView.frame.origin.x;
NSInteger baseY = superView.frame.origin.y;
NSMutableArray *imageArr = [[NSMutableArray alloc] initWithCapacity:rowNum*colNum];
UIImage *superImage = [self viewConvertToImage:superView];
for (int i = 0; i < rowNum*colNum; i++) {
UIImageView *subImageView = [[UIImageView alloc] initWithFrame:CGRectMake(baseX + (i%colNum)*width, baseY+(i/rowNum)*height, width, height)];
//从UIImage中切出CGImage,再进行转化
CGImageRef cgImage = CGImageCreateWithImageInRect(superImage.CGImage, CGRectMake((i%colNum)*width, (i/rowNum)*height, width, height));
subImageView.image = [UIImage imageWithCGImage:cgImage];
[imageArr addObject:subImageView];
}
return imageArr;
}
相关文章推荐
- xcode6模拟器UITextField不能自动弹出键盘
- OSGI Blueprint(2)
- NDK编译问题: Program "ndk-build" is not found in PATH 和 Unable to launch cygpath. Is Cygwin on the path?
- Win10 UWP开发中的重复性静态UI绘制小技巧 2
- HDU 5273 Dylans loves sequence(线段树求逆序数对+离散化)
- UITableViewcell autolayout下动态高度
- 判断UITextField.text是否为空
- Introduction to Guid ( globally unique identifier )
- 嵌套循环中的 continue ,break
- OSGI Blueprint
- leetcode Repeated DNA Sequences 哈希表
- [SoapUI] 在某个测试步骤下面增加Script Assertion,运用 messageExchange 获取response content
- Functional MRI (second edition) -- 5. MR Contrast Mechanisms and Pulse Sequences
- easyui在ie下要注意的问题
- 芸芸毕业生
- 线性布局
- Assertion failure in -[UICGColor encodeWithCoder:]
- iOS隐藏UITableview下面多余的tableviewcell的线条
- iOS开发:UITableView加载多张照片导致内存上涨的问题
- 相对布局