I am developing iphone app in which user can add customize the photo by adding text on it. Now I need to convert this photo into 5" height by 4.5" width in iOS7.
I am taking the screenshot of the the view as below to combine photo & labels added to it as below
-(UIImage*)customizedImageMain
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.containerView.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.containerView.bounds.size);
[self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
I checked the image from iPod touch with 4" screen. The image size that I get from above code is nearly 4.445" width & 7.889" height.
Now how can I resize this image to exact 5" height & 4.5" width irrespective of device in which app is running ?
Thanks in advance.
UPDATED Code as per Vin's solution
-(UIImage*)customizedImageFirst
{
// We want 5" * 4.5" image which can be represented as 1630 * 1458
// 1630 pixels = 5 inch * (326 pixels / 1 inch)
// 1458 pixels = 4.5 inch * (326 pixels / 1 inch) in terms of pixels.
CGSize requiredImageSize = CGSizeMake(1458.0,1630.0);
UIGraphicsBeginImageContext(requiredImageSize);
[self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
[combinedImage drawInRect:CGRectMake(0,0,requiredImageSize.width,requiredImageSize.height)];
UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
Using above code I am getting this result
Now if I use code without considering UILabels added to customize as below
-(UIImage*)customizedImageSecond // If I use your code
{
// We want 5" * 4.5" image which can be represented as 1630 * 1458
// 1630 pixels = 5 inch * (326 pixels / 1 inch)
// 1458 pixels = 4.5 inch * (326 pixels / 1 inch) in terms of pixels.
CGSize requiredImageSize = CGSizeMake(1458.0,1630.0);
UIGraphicsBeginImageContext(requiredImageSize);
[self.myImageView.image drawInRect:CGRectMake(0,0,requiredImageSize.width,requiredImageSize.height)];
UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
, then I am getting this image
My UI hierarchy
Main View of UIViewController. I have added another UIView (containerView) in the main view. In this containerView I have added my UIImageView & UILabels. So get the customized image with labels added on it I am taking the screenshot of containerView.
This is the custom method that i generally use to scale the image to the desired size. This method take the newSize
(For Eg : CGSize newSize = CGSizeMake(30.0, 30.0);
) as one of the argument. Here, 30.0
is the value in terms of pixels.
+(UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
So, you can scale the image in terms of pixels using the above method. But you want to scale the image in terms of inches. A display device has a limited number of pixels it can display, and a limited space over which to display them.
Pixel Density is a measurement of the resolution of devices and can be calculated in terms of PPI(Pixels per inch) or PPCM(Pixels per centimeter).
In your case, you want 5" * 4.5" image which can be represented as 1630 * 1458 (1630 pixels = 5 inch * (326 pixels / 1 inch), 1458 pixels = 4.5 inch * (326 pixels / 1 inch))
in terms of pixels.
Note : PPI for Apple iPhone and iPod Touch above 4th Generation is 326.
So, If you scale the image with size of 1630 * 1458
then you will get the image of 5"" * 4.5"
. For that you have to pass CGSize newSize = CGSizeMake(1630.0, 1458.0);
in above method.