捕获屏幕

时间:2011-09-15 11:13:40

标签: iphone objective-c ios screenshot

我正在尝试捕获(屏幕截图)视图。为此,我使用下面显示的一段代码将其作为PNG图像保存到我的文档目录中。

UIGraphicsBeginImageContextWithOptions(highlightViewController.fhView.centerView.frame.size, YES, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"1.png"];
NSData *imageData = UIImagePNGRepresentation(screenshot);
[imageData writeToFile:appFile atomically:YES];
UIGraphicsEndImageContext();

问题:我可以捕获部分视图吗?因为在上面的代码我不能改变原点(框架)。如果有人有其他方法来捕获视图的特定部分,请分享。

4 个答案:

答案 0 :(得分:6)

你可以裁剪图像: http://iosdevelopertips.com/graphics/how-to-crop-an-image.html

CGRect rect = CGRectMake(0,0,10,10);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

答案 1 :(得分:1)

试试这段代码。这肯定有效,因为我已经在我的许多项目中实现了它:

- (UIImage *)image
{
    if (cachedImage == nil) {
        //YOU CAN CHANGE THE FRAME HERE TO WHATEVER YOU WANT TO CAPTURE
        CGRect imageFrame = CGRectMake(0, 0, 400, 300); 
        UIView *imageView = [[UIView alloc] initWithFrame:imageFrame];
        [imageView setOpaque:YES];
        [imageView setUserInteractionEnabled:NO];

        [self renderInView:imageView withTheme:nil];        

        UIGraphicsBeginImageContext(imageView.bounds.size);
            CGContextRef c = UIGraphicsGetCurrentContext();
            CGContextGetCTM(c);
            CGContextScaleCTM(c, 1, -1);
            CGContextTranslateCTM(c, 0, -imageView.bounds.size.height);
            [imageView.layer renderInContext:c];
            cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];

            // rescale graph
            UIImage* bigImage = UIGraphicsGetImageFromCurrentImageContext();
            CGImageRef scaledImage = [self newCGImageFromImage:[bigImage CGImage] scaledToSize:CGSizeMake(100.0f, 75.0f)];
            cachedImage = [[UIImage imageWithCGImage:scaledImage] retain];
            CGImageRelease(scaledImage);
        UIGraphicsEndImageContext();

        [imageView release];
    }

    return cachedImage;
}

我希望这会对你有所帮助。

答案 2 :(得分:0)

看看你是否可以像这样指定矩形,然后截取屏幕截图。

CGRect requiredRect = CGRectMake(urView.frame.origin.x, urView.frame.origin.y, urView.bounds.size.width, urView.bounds.size.height);
UIGraphicsBeginImageContext(requiredRect.size);

您可以更改原点并查看其是否有效。 如果这不起作用,您可以尝试按@mcb

所述裁剪图像

答案 3 :(得分:0)

您可以使用此代码

UIGraphicsBeginImageContext(self.view.bounds.size);

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

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect;
rect = CGRectMake(250,61 ,410, 255);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
CGImageRelease(imageRef);