ScreenShot图像 - 拍摄点的周围区域

时间:2014-12-16 23:08:46

标签: ios uiimage core-graphics cgcontext

我已经实现了双击手势识别器,一旦用户点击两次,就会在该点上添加注释。此外,我想裁剪一个节拍区域的一部分。但是每次它返回不同大小的屏幕。

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateRecognized) {
        // handling code      
        point = [sender locationInView:sender.view];
        iViewController.noteImageView.image=[self captureView:self.view];
        [self.view addSubview:iViewController.view];
    }
}

 -(UIImage*)captureView:(UIView*)view
    {
        CGRect screenRect=CGRectMake(point.x-20,point.y-40,point.x+10,point.y+20);
        //CGRect screenRect=[[UIScreen mainScreen]bounds];
        UIGraphicsBeginImageContext(screenRect.size);

        CGContextRef ctx= UIGraphicsGetCurrentContext();
        [[UIColor blackColor]set];
        CGContextFillRect(ctx, screenRect);
        [view.layer renderInContext:ctx];

        UIImage *scImage=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return scImage;

    }

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

-(UIImage*)captureView:(UIView*)view forPoint:(CGPoint)point
{
    CGRect cropRect = CGRectMake(point.x-20,point.y-40,point.x+10,point.y+20);
    UIGraphicsBeginImageContext(view.bounds.size);
    CGContextRef ctx= UIGraphicsGetCurrentContext();

    [[UIColor blackColor] setFill];
    CGContextFillRect(ctx, view.bounds);
    [view.layer renderInContext:ctx];
    CGImageRef snapshot = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());

    CGImageRef cropped = CGImageCreateWithImageInRect(snapshot, cropRect);
    UIImage *image =  [UIImage imageWithCGImage:cropped];

    UIGraphicsEndImageContext();
    CGImageRelease(snapshot);
    CGImageRelease(cropped);

    return image;
}