如何截取uiview的特定区域的屏幕截图

时间:2014-04-30 06:33:53

标签: ios objective-c uiview

    UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
    return viewImage;

这是我正在使用的代码,我需要截取uiview特定区域的截图

5 个答案:

答案 0 :(得分:2)

这是获取特定区域的屏幕截图并保存在NSDocumentDirectory中的代码。

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef img = [viewImage CGImage];
img = CGImageCreateWithImageInRect(img, CGRectMake(0,60, 320, 330));  // This is the specific frame size whatever you want to take scrrenshot
viewImage = [UIImage imageWithCGImage:img];

//writeToFile----------------

NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath=[path objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager] ;

BOOL isDir ;
imagePath =[imagePath stringByAppendingPathComponent:@"PicPanleImage"];
if(YES == [fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
{

}
else
{
    [fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:NO attributes:nil error:nil];
}

imagePath =[imagePath stringByAppendingPathComponent:@"Image.jpeg"];
NSData *tempImageData=[NSData dataWithData:UIImagePNGRepresentation(viewImage)];
[tempImageData writeToFile:imagePath atomically:YES];

答案 1 :(得分:1)

您没有保存图像。试试这个

UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width,self.view.size.height));
CGContextRef context =UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil,nil);

答案 2 :(得分:0)

检查以下代码。

- (UIImage *)snapshot
{
    UIImage* snapShot = nil;
    UIGraphicsBeginImageContext(snapShotView.frame.size);
    {
        [snapShotView.layer renderInContext: UIGraphicsGetCurrentContext()];
        snapShot = UIGraphicsGetImageFromCurrentImageContext();

    }

    UIGraphicsEndImageContext();
    return snapShot;

}

答案 3 :(得分:0)

捕获UIView:

CGRect rect = [targetView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[captureView.layer renderInContext:context];   
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

捕获UIWebView:

UIGraphicsBeginImageContext(targetWebView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

保存到SavedPhotosAlbum:

UIImageWriteToSavedPhotosAlbum(capturedImage, nil, nil, nil);

jpg格式另存为应用的文档文件夹:

NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];    
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];

答案 4 :(得分:0)

假设您正在使用图像,则可以使用此代码。

    UIImageView *v = [[UIImageView alloc]initWithImage:YourImage];
    UIGraphicsBeginImageContext(v.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    [v drawRect:CGRectMake(0,0,width,height)];
    CGContextRestoreGState(context);
    UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();

    float actualHeight = image1.size.height;
    float actualWidth = image1.size.width;
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = self.view.frame.size.width/self.view.frame.size.height;
    if(imgRatio!=maxRatio){
        if(imgRatio < maxRatio){
            imgRatio = self.view.frame.size.height / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = self.view.frame.size.height;
        }else{
            imgRatio = self.view.frame.size.width / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = self.view.frame.size.width;
        }
    }

    CGRect rect = CGRectMake(0.0, 0.0, actualWidth,(actualHeight+30));
    UIGraphicsBeginImageContext(rect.size);
    [image1 drawInRect:rect];

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString *imageStr;
    UIGraphicsBeginImageContext(img.size);
  //  [img drawAtPoint:CGPointMake(0, 10)];

    [img drawInRect:CGRectMake(0, 20, 340, 410)];