1.要求:服务器给一张图片模板,要在模版上镂空,然后添加一些别的图片,然后组合成一张图,这个模版的像素 不是固定的,有可能比 当前手机屏幕大。所以,在组合截图的时候,有一定的要求。
贴代码:
1 /** 2 * 组合图片 imgVC_bg :模版image存放的uiimageview , array_imageView 镂空图片视图,double_proportion:模版图片放大后 和 当前屏幕宽或高的比例 3 */ 4 #pragma makr -组合图片 5 -(void )SaveImageViewWithMoBan 6 { 7 UIGraphicsBeginImageContext(imgVC_bg.image.size);//截取图片内容的大小,这里为了保证像素是原来像素,一定要用image的size 8 9 [imgVC_bg.image drawInRect:CGRectMake(0, 0, imgVC_bg.image.size.width, imgVC_bg.image.size.height)];//不用说了,draw的时候也是一样 10 for (UIImageView *imgvc in array_imageView) {//循环 draw。这里是除以比例,这里的偏移量原本乘了比列,现在要还原成本身的大小,肯定是除以比例啦 11 UIImage *img=imgvc.image; 12 [img drawInRect:CGRectMake(imgvc.frame.origin.x/double_proportion,imgvc.frame.origin.y/double_proportion,imgvc.frame.size.width/double_proportion,imgvc.frame.size.height/double_proportion)]; 13 14 } //重新创建一个图片,图片的大小一定要是原模版图的大小 15 CGImageRef NewMergeImg = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage, 16 CGRectMake(0, 0, imgVC_bg.image.size.width, imgVC_bg.image.size.height)); 17 UIGraphicsEndImageContext(); 18 19 [imgVC_bg removeFromSuperview]; 20 { 21 UIImage *img=[UIImage imageWithCGImage:NewMergeImg]; 22 UIImageView *imgvc=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)]; 23 [imgvc setImage:img]; //打印原模版图的size,看是否有变 24 NSLog(@"img_width=%lf img_height=%lf",img.size.width,img.size.height); 25 [vContainer addSubview:imgvc]; 26 } 27 [kAPPDELEGATE.navigationController popViewControllerAnimated:YES]; 28 [[NSNotificationCenter defaultCenter]postNotificationName:@"EditImage" object:[UIImage imageWithCGImage:NewMergeImg] userInfo:nil]; 29 }
时间: 2024-11-29 09:13:28