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

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

最佳答案

这是用于截取特定区域的屏幕截图并保存在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];

关于ios - 如何截取uiview特定区域的屏幕截图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23381206/

10-13 04:05