截图与图片合成的几种方法

 

-(UIImage *)screenShots
{
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != &UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    }
    else
    {
        UIGraphicsBeginImageContext(imageSize);
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            CGContextConcatCTM(context, [window transform]);
            CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
            
            [[window layer] renderInContext:context];
            
            CGContextRestoreGState(context);
        }
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    NSLog(@"Suceeded!");
    return image;
}

 

    UIGraphicsBeginImageContextWithOptions(self.zoomScrollView.size , NO, 1);

    [self.zoomScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    //把截图保存到相册里

下面总结了几种截图 和图片合成的方法 

//第二种多图片绘图方法 高清 
-(UIImage *)getSubImage3
{
    CGSize size = self.img_content.image.size;
    CGFloat scale =  self.img_content.image.size.width/self.img_content.frame.size.width;
    CGSize canvasSize = size;
    UIGraphicsBeginImageContext(canvasSize);
    
    //Draw image2
    [self.img_content.image drawInRect:CGRectMake(0, 0, canvasSize.width, canvasSize.height)];
    
    [self.texiaoView.subviews enumerateObjectsUsingBlock:^(UIImageView * imageview, NSUInteger idx, BOOL *stop) {
        if ([[imageview class] isSubclassOfClass:[UIImageView class]]) {
            //            CGSize canvasSize2;
            UIImage * image = imageview.image;
            CGRect frame = imageview.frame;
            CGPoint offset = frame.origin;
            offset.x = scale * offset.x;
            offset.y = scale * offset.y;
            CGSize drawsize;
            drawsize.width = scale*image.size.width;
            drawsize.height =scale* image.size.height;
            [image drawInRect:CGRectMake(offset.x, offset.y, drawsize.width, drawsize.height)];
        }
    }];
    
    UIImage *resultImage=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultImage;
}

//用绘图的模式获得图片 模糊
-(UIImage *)getSubImage2{
    
    CGSize size = self.img_content.frame.size;
    CGSize canvasSize = size;
    CGPoint offset1;
    offset1.x = (canvasSize.width - size.width) / 2.0f;
    offset1.y = (canvasSize.height - size.height) / 2.0f;
    UIGraphicsBeginImageContextWithOptions( canvasSize, NO, 1);
    
   [[self.img_content.image resize:size] drawAtPoint:offset1 blendMode:kCGBlendModeNormal alpha:1.0f];
    
    [self.texiaoView.subviews enumerateObjectsUsingBlock:^(UIImageView * imageview, NSUInteger idx, BOOL *stop) {
        if ([[imageview class] isSubclassOfClass:[UIImageView class]]) {
//            CGSize canvasSize2;
            UIImage * image = imageview.image;
            CGRect frame = imageview.frame;
            CGPoint offset = frame.origin;
//            canvasSize2.width = fmaxf(size.width, image.size.width);
//            canvasSize2.height = fmaxf(size.height, image.size.height );
            
            [image drawAtPoint:offset blendMode:kCGBlendModeNormal alpha:1.0f];
            
        }
    }];
    
    UIImage * result = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return result;
}

//用截图的模式获得图片  有模糊
-(UIImage *)getSubImage{
    //    self.originalImage = self.showImgView.image;
 
    UIGraphicsBeginImageContext(self.BgView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    UIRectClip(CGRectMake(0, 0, self.BgView.width, self.BgView.height));
    [self.BgView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

 

转载于:https://www.cnblogs.com/nonato/p/4460595.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值