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.
Related
I am resizing image but after resizing image is not returning in good quality here is my code
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
here is my code how it is call
UIImage *image = [self imageWithImage:self.selectedImage scaledToSize:CGSizeMake((self.imageView.image.size.width*self.slider.value), (self.imageView.image.size.height*self.slider.value))];
result of image
Here is the small image that i am scaling.
You should use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext.
For example,
UIGraphicsBeginImageContextWithOptions(signView.bounds.size, signView.opaque, 0.0);
[signView.layer renderInContext: UIGraphicsGetCurrentContext()];
// UIImage *imgMySignature = UIGraphicsGetImageFromCurrentImageContext();
signView.img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I am rendering image from UIView i.e signView. You can use something like this.
I am having problem with UIImage resizing, image masking is working fine but after applying mask UIImage is starched, the problem is with scaling as image is not scaled properly.
CCClippingNode *clippingNode = [[CCClippingNode alloc] initWithStencil:pMaskingFrame ];
pTobeMasked.scaleX = (float)pMaskingFrame.contentSize.width / (float)pTobeMasked.contentSize.width;
pTobeMasked.scaleY = (float)pMaskingFrame.contentSize.height / (float)pTobeMasked.contentSize.height;
clippingNode.alphaThreshold = 0;
[pContainerNode addChild:clippingNode];
pTobeMasked.position = ccp(pMaskingFrame.position.x, pMaskingFrame.position.y);
[clippingNode addChild:pTobeMasked];
One of my project I have used below function to resize an image;
/*
method parameters definition
image : original image to be resized
size : new size
*/
+ (UIImage*)resizeImage:(UIImage *)image size:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
This will work like a charm. It's similar to the already posted answer, but it has some more options:
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
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];
I am not sure whether I can improve image quality but following code displays very poor image quality in PDF. I know its standard code to generate images from view but is there anything I could do to specify image quality or improve it?
- (void)renderView:(UIView*)view {
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewAsImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[viewAsImage drawInRect:rect];
}
You probably need to create a graphics context with a scale of 2 (retina) instead of the default 1. To do so, use UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0);. This will create an image context with an opaque target (you can set the second parameter to NO if you're rendering a transparent image) and with a scale factor of your device's main screen.
Double the size!
- (void)renderView:(UIView*)view {
CGSize newSize = view.frame.size;
newSize.width = newSize.width * [UIScreen mainScreen].nativeScale;
newSize.height = newSize.height * [UIScreen mainScreen].nativeScale;
UIGraphicsBeginImageContext(newSize);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewAsImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[viewAsImage drawInRect:rect];
}
I have a UIImageView with a size of (144,130) and the image size I am getting after capturing is (720,960).
I have resized image with
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Here new size is (144,130) which I have given because it is the imageviews size. Even though the image is getting stretched.
What should I make changes so that image does not get stretched?
EDIT-1
UIImage *imageCapture = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImage *finalImage = [self imageByScalingAndCroppingForSize:(imageview.frame.size)];
as per the answer in this link
EDIT-2
Image captured has size : {720, 960}
imageview.frame.size : {144, 130}
newSize : {97.5, 130}
with resizing u have to maintain the aspect ratio. just match your view's aspect ratio with the actual image and there won't be any stretched image...
this could be useful for you
You have set the Imageview Property
imageView.contentMode=UIViewContentModeScaleAspectFit;
and if you are using nib then set the imageview property in View section-> Mode -> Aspect Fit
There is no need to resize your image. It worked for me.
And though you want to resize the image you can have following image in UIImage category
- (UIImage *) scaleProportionalToSize: (CGSize)size
{
float widthRatio = size.width/self.size.width;
float heightRatio = size.height/self.size.height;
if(widthRatio > heightRatio)
{
size=CGSizeMake(self.size.width*heightRatio,self.size.height*heightRatio);
} else {
size=CGSizeMake(self.size.width*widthRatio,self.size.height*widthRatio);
}
return [self scaledToSize:size];
}
- (UIImage *)scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[newImage autorelease];
return newImage;
}
When you are resizing image to newSize you must take into account that, if you are resize width by suppose 10% then you must resize height by 10% only.