Resizing screen shot image leads to blurred text ios - ios

My code:
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
The first parameter is image which is screen shot my view controller.
The second parameter newSize is actually smaller than the image size which follows the aspect ratio also. But the image is looks good but the text(UILabel) are some what blur.
How can i solve this any idea?

Assuming newSize is in points:
UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale);

Related

Add Width to a UIImage?

I have an image 96x96 and I would like to add extra width to it so that the new image is 120 x 96 with the image centered.
Above is an example of what I have. Below is what I want (Added width with the original image centered in it).
I've tried the following, but I get a weirdly cropped image:
- (UIImage*)imageWithAddedWhitespaceFromImage:(UIImage *)image {
CGSize size = CGSizeMake(96, 96);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(12, 0, size.width + 24, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
You're almost there. You need to create an image context of target size and then draw your image at original size. Comments in the code with ** highlight my changes.
- (UIImage*)imageWithAddedWhitespaceFromImage:(UIImage *)image {
// ** Create context at target size of 120x96
CGSize size = CGSizeMake(120, 96);
// ** Use this API for properly scaled image (instead of UIGraphicsGetImageFromCurrentImageContext)
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
// ** Now draw the image offset by 12px
[image drawInRect:CGRectMake(12, 0, image.size.width, image.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

How to resize an image which has transparent portions in objective C

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];

unable to resize image properly

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;
}

Scale up UIView with UIImage without affecting resolution of UIImage

My problem is when I'm scaling up my UIView's background image it gets anti-aligning and very blurry although the original image is in high resolution and very bright.
The process of scaling happens when I make an animation on the UIView to scale it. I'm scaling UIView using the transform property of UIView.
The code I'm using to make the UIImage fit into new scale is:
-(UIImage *)imageResize :(UIImage*)image withSize:(CGSize)size
{
CGSize newSize = self.bounds.size;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
And I'm assigning the background color with this line:
self.backgroundColor = [UIColor colorWithPatternImage:[self imageResize:img withSize:(CGSize)size]];
I guess you should try to apply screen resolution in this line:
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
Should look like this
CGFloat screenScale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(newSize, NO, screenScale);

Image gets stretched when captured

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.

Resources