Objective C Resize image as 2x image - ios

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.

Related

How to capture a UIView as image which is out of currently visible frame?

Is there any way to capture a UIView to UIImage which is not currently visible or it is out of the frame. Below the code I am using now:
UIGraphicsBeginImageContextWithOptions(_myView.bounds.size, _myView.opaque, 0.0f);
[theView drawViewHierarchyInRect:_myView.bounds afterScreenUpdates:NO];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Try this-
-(UIImage *)imageFromView:(UIView *)myView{
UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, [UIScreen mainScreen].scale);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Default iPhone screenshot programmatically

I tried two different ways to create a screenshot, but unfortunately they don't work as I need, I have an RMMapView that is blank on the screenshot. When I create snapshot manually on my device it works perfectly, and the map view is on the screen. So I would like to achieve the same result programmatically. Is it possible somehow as I tried? Or what is the right way to do that? (To reproduce that type of screenshot)
- (UIImage *) takeScreenshot {
//1. version
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
//2. version
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Actually You can use a method called takeSnapshot in a RMMapView.
UIImage *image = self.mapView.takeSnapshot
[Update]
Can you try this method instead
CGSize size = self.view.bounds.size;
CGRect cropRect = self.mapView.bounds
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
UIImage * cropImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);
UIGraphicsEndImageContext();
Good luck

How to select a region of an UIImage and then create a new one out of it?

is there an easy way of selecting a region of an UIImage Instance and then creating a new image out of it? I tried it using a pan-gesture Recognizer for getting the coordinates that the user selected. But with this option there is now visible feedback and i also have to transform the coordinates. Is there maybe a nice plugin or best practices for doing this ?
try this:
UIGraphicsBeginImageContext(self.view.bounds.size);
// retrieve the current graphics context
CGContextRef context = UIGraphicsGetCurrentContext();
// render view into context
[self.view.layer renderInContext:context];
// create image from context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
image=[self cropImage:image];
UIGraphicsEndImageContext();
- (UIImage *)cropImage:(UIImage *)oldImage {
CGSize imageSize = oldImage.size;
UIGraphicsBeginImageContextWithOptions(CGSizeMake( imageSize.width,imageSize.height - 150),NO,0.); //set your height and width
[oldImage drawAtPoint:CGPointMake( 0, -80) blendMode:kCGBlendModeCopy alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
Like this:
CGImageRef imageRef = CGImageCreateWithImageInRect([bigImage CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

iOS capture UIView with UIImageView inside

I'm trying to get a UIView screen capture on iOS.
In my view I have two UIImageView with animations.
The code that I using it to capture the view is:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Read the UIImage object
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:didFinishSavingWithError:contextInfo:), nil);
The result is an image with the background and only one UIImageView. Both ImageViews are in moviming all the time. If I take several pictures, then the UIImageView is in the same position each time.
Try this one:
-(CGImageRef)imageCapture
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect= CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
return imageRef;
}
use the below line whenever you want to capture the screen
UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];

IOS image scaling for thumbnail

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.

Resources