This question already has answers here:
UIImage screenshot loses its quality
(5 answers)
Closed 8 years ago.
What I'm trying to achieve is taking a screenshot in my app which is working fine, but the quality is very poor, and the size of the screenshot is also pretty small. I also try to crop the bottom of the image as I did for the top, but I cannot see where I can add dimensions for the bottom. Is there a better way to do this? I'm using the code below:
// Define the dimensions of the screenshot you want to take (the entire screen in this case)
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//now we will position the image, X/Y away from top left corner to get the portion we want
UIGraphicsBeginImageContext(self.view.frame.size);
[sourceImage drawAtPoint:CGPointMake(0, -10)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
Thank you for your answer.
replace
UIGraphicsBeginImageContext(self.view.bounds.size);
with
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
This is the code I use to save the contents of a view to 2 png files (low and high resolution) onto the desktop.
// High resolution
UIGraphicsBeginImageContextWithOptions(drawView.bounds.size, NO, 0.0);
[[drawView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *ViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *pngData = UIImagePNGRepresentation(ViewImage);
[pngData writeToFile:#"/Users/_username_/Desktop/Image#2x.png" atomically:NO];
// Low resolution
UIGraphicsBeginImageContext(drawView.bounds.size);
[[drawView layer] renderInContext:UIGraphicsGetCurrentContext()];
ViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pngData = UIImagePNGRepresentation(ViewImage);
[pngData writeToFile:#"/Users/_username_/Desktop/Image.png" atomically:NO];
Related
This is my Transparent image (Front image).
This is my selected image (Back Image).
I am giving gestures to the back image.
How can i save my image after using of pan, pinch, rotate gestures.
Present am able to combine both images but back image is saving as it is like (below image)
Using this code
CGSize newSize = CGSizeMake(640, 960);
UIGraphicsBeginImageContext( newSize);
[backImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frontImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(finalImage, nil, nil, nil);
I think i should handle this two lines but i don't know how to handle.
[backImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frontImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
Can any one help me will highly appreciated.
Thank you.
Please find the below code to get snapshot your imageview.
UIGraphicsBeginImageContextWithOptions(yourImageViewToSnapShot.bounds.size, NO, 0.0);
[yourImageViewToSnapShot.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I am trying to resize a PNG which has transparent sections, first I used:
UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);
[sourceImage drawInRect:CGRectMake(0,0, newSize.width, newSize.height)];
UIImage* targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
but the resultant image is opaque. Then I read that drawInRect by defaults draws the image as opaque, so I modified the drawInRect line to:
[fromImage drawInRect:CGRectMake(0,0, newSize.width, newSize.height) blendMode:kCGBlendModeNormal alpha:0.0];
The resultant image is blank, I think there should be some combination of parameters in the drawInRect that will retain the transparency of the image.
I have searched other threads, and everywhere I see the generic resizing code, but nowhere it talks about images with transparent portions.
Anybody has any Idea ?
CGRect rect = CGRectMake(0,0,newsize.width,newsize.height);
UIGraphicsBeginImageContext( rect.size );
[sourceImage drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(picture1);
textureColor=[UIImage imageWithData:imageData];
Basically I have a main UIImage, which acts as a background/border. Within that UIImage I have 2 more UIImages, vertically split with a gap around them so you can still see a border of the main background UIImage. On each side I have a UILabel, to describe the images. Below is a picture of what I mean to help put into context.
What I want to achieve is to make this into 1 image, but keeping all of the current positions, layouts, image layouts (Aspect Fill) and label sizes and label background colours the same. I also want this image to be the same quality so it still looks good.
I have looked at many other stackoverflow questions and have so far come up with the follow, but it has the following problems:
Doesn't position the image labels to their correct places and sizes
Doesn't have the background colour for the labels or main image
Doesn't have the images as Aspect Fill (like the UIImageViews) so the outside of each picture is shown as well and isn't cropped properly, like in the above example.
Below is my code so far, can anyone help me achieve it like the image above please? I am fairly new to iOS development and am struggling a bit:
-(UIImage *)renderImagesForSharing{
CGSize newImageSize = CGSizeMake(640, 640);
NSLog(#"CGSize %#",NSStringFromCGSize(newImageSize));
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);
[self.mainImage.layer renderInContext:UIGraphicsGetCurrentContext()];
[self.beforeImageSide.image drawInRect:CGRectMake(0,0,(newImageSize.width/2),newImageSize.height)];
[self.afterImageSize.image drawInRect:CGRectMake(320,0,(newImageSize.width/2),newImageSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.beforeLabel drawTextInRect:CGRectMake(60.0f, 0.0f, 200.0f, 50.0f)];
[self.afterLabel drawTextInRect:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
NSData *imgData = UIImageJPEGRepresentation(image, 0.9);
UIImage * imagePNG = [UIImage imageWithData:imgData]; //wrap UIImage around PNG representation
UIGraphicsEndImageContext();
return imagePNG;
}
Thank you in advance for any help guys!
I don't understand why you want use drawInRect: to accomplish this task.
Since you have the images and everything with you, you can easily create a view as you have shown in the image. Then take a screenshot of it like this:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData*theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too
[theImageData writeToFile:#"example.jpeg" atomically:YES];
Change the self.view to the view just created
It will give some idea.
UIGraphicsBeginImageContextWithOptions(DiagnosisView.bounds.size, DiagnosisView.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor redColor] set];
CGContextFillRect(ctx, DiagnosisView.frame);
[DiagnosisView.layer renderInContext:ctx];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *imagePath = [KdiagnosisFolderPath stringByAppendingPathComponent:FileName];
NSData *pngData = UIImagePNGRepresentation(img);
[pngData writeToFile:imagePath atomically:YES];
pngData = nil,imagePath = nil;
I am using NinevehGL 3D engine http://nineveh.gl/
I want to get screenshot of OpenGL object. Problem is with below code
-(UIImage *) screenshot
{
// Capture screen here... and cut the appropriate size for saving and uploading
UIGraphicsBeginImageContext([self theNGLView].bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// crop the area you want
CGRect rect;
rect = CGRectMake(0, 10, 300, 300); // whatever you want
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
Only background comes in screenshot but not actual NGLView or OpenGL object which is drawn on top of it.
I am using iOS SDK 6.0
tried using
[[self the NGLView].layer renderInContext:UIGraphicsGetCurrentContext()];
?
Please go through this link, you need to download the latest NinevehGL SDK for iPhone5/iOS 6 which is provided in their twitter link.
I tried to resize image to fit the frame with the code below but the image is kind of distorted.
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UIImage* image =[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
deal.Image=[tfbImageHelper imageWithImage:image scaledToSize:newSize];
Yes, because you should use UIGraphicsBeginImageContextWithOptions and the last argument should be 0.0f (which means, to keep the current scale). It's distorted, because you're testing it on a retina display and the scale factor there is 2. Also make sure, you are keeping the aspect ratio, when assigning the new size.