Resolution of capture screen image is not retina - iphone

I am using the following code for capture a view on the screen but it is not as sharp as on the screen. the imageView size is 200x200point but the scale is 2.0 (with retina screen). The saved img size is 200x200 px. how could i make the img as sharp as the original one? Any help will be appreciated!
- (UIImage*)captureView:(UIView *)theView {
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

Try like that:
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;

You are using the old way of creating the image context. Use this instead:
UIGraphicsBeginImageContextWithOptions(rect.size, YES, [UIScreen mainScreen].scale);
The older UIGraphicsBeginImageContext function always assumes a scale of 1.0.
Replace 'YES' with 'NO' if you need the alpha channel.

Related

image is blurred after cropping the image?

I have an application in which i am cropping the image taken from the camera.all are going well.but after the cropping the image seems to blured and streched.
CGRect rect = CGRectMake(20,40,280,200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y,280,200);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
[image drawInRect:drawRect];
// grab image
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size = [croppedImage size];
NSLog(#" = %#",NSStringFromCGSize(size));
NSData* pictureData = UIImagePNGRepresentation(croppedImage);
Can anybody help me in finding out where i am going wrong?
try replacing
UIGraphicsBeginImageContext(rect.size);
with
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
to account for retina

UIImage screenshot loses its quality

I am merging two images and then I take a screenshot by applying this code:
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_AddText=viewImage;
[dragView removeFromSuperview];
imgV_SelectedImg.image=nil;
imgV_SelectedImg.image=img_AddText;
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
The problem is that when the final image loses its quality it blurs.
Try using the withOptions version of UIGraphicsBeginImageContext
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
i got the snapshot with good quality and particular location of screen. By this code.
-(UIImage *)takeScreenShot
{
CGRect grabRect;
grabRect = CGRectMake(0,70,320,260);
UIGraphicsBeginImageContextWithOptions(grabRect.size, self.view.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
[self.view.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
it gives me excellent snapshot..
I've made a category on UIImage class that may help you. It goes like this:
+ (UIImage*)imageWithView:(UIView *)view opaque:(BOOL)opaque bgColor:(UIColor*)bgColor{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, [[UIScreen mainScreen] scale]);
if(!opaque){
[bgColor set];
}
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
It works fine for me. No bluring was detected. Try to use it. If you'll still have it then most likely the problem is in your saving code...
Cheers... :)
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
this solve my prblem by increasing scale from 1.0 to 2.0
Did you provide an image for retina display? You should check it. You might be running in a simulator (in retina).

Capturing CGRect does not give proper image. What to do?

I am capturing CGRect with following code. But the resulting image is not the image what i want. Image has some transparent background. What to do for removing transparent background as suggesting the picture.
- (UIImage *)captureScreenInRect:(CGRect)captureFrame {
CALayer *layer;
layer = imageScrollview.layer;
UIGraphicsBeginImageContext(imageScrollview.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
\[layer renderInContext:UIGraphicsGetCurrentContext()\];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}
Translate your context so that its origin matches your captureFrame:
UIGraphicsBeginImageContext(imageScrollview.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, -captureFrame.origin.x, -captureFrame.origin.y);
[imageScrollView.layer renderInContext:c];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
(written from memory, untested)
Additionally clipping the context is not necessary as the image is already clipped by the image context's bounds.
Try this one
CGRect cropRect = CGRectMake(imageScrollview.frame.origin.x+15, imageScrollview.frame.origin.y+15, WIDTH, HEIGHT);

iPhone: UIImage obtained after rendering a view is solid black

I'm using this method to render a UIView into a UIImage:
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
The resulting image is of the right size (as shown by the UIImage's size property), but its contents is solid black colour. The view passed there definitely contains some graphics, but it's not rendered. Any idea why?
This is the code I use to capture images of a UIView:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
Your code looks fine to me. You can try swapping mine out to see if it helps, but I'd cite it to be a separate issue from the code you posted.

Problem about #2X

1.I use below code for take screenshot.is it necessary to change it for high resolution
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2.what does happen if i just use one image with 640*960 resolution for both low and high quality ? (means that don't use image with #2x)
That will only take normal (320x480) resolution screenshots. So in answer to 1, yes. Older generation devices can do double resolution too, the screen just can't display it.
To take a screenshot at the res of the device you can do this:
CGSize size = self.view.bounds.size;
CGFloat scale = 1.0f;
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
size = CGSizeApplyAffineTransform(size, CGAffineTransformMakeScale(scale, scale));
}
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, scale, scale);
[self.view.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Taking double res on older devices does work, and will be scaled back down if displayed on screen. But is not recommended, and simply a waste.