Reputation: 11
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
This is the code i am using i need to take screenshot of a specific area of uiview
Upvotes: 1
Views: 1434
Reputation: 343
Suppose that you are using an image then you can use this code.
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)];
Upvotes: 0
Reputation: 1849
You are not saving the Image. Try This
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);
Upvotes: 1
Reputation: 814
This is the code for taking the screenshot for specific area and save in 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];
Upvotes: 2
Reputation: 31339
To capture UIView:
CGRect rect = [targetView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[captureView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
To capture UIWebView:
UIGraphicsBeginImageContext(targetWebView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Save to SavedPhotosAlbum:
UIImageWriteToSavedPhotosAlbum(capturedImage, nil, nil, nil);
Save as jpg
format into the app's document folder:
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];
Upvotes: 0
Reputation: 726
Check the following code.
- (UIImage *)snapshot
{
UIImage* snapShot = nil;
UIGraphicsBeginImageContext(snapShotView.frame.size);
{
[snapShotView.layer renderInContext: UIGraphicsGetCurrentContext()];
snapShot = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
return snapShot;
}
Upvotes: 0