How to crop UIImage with fixed dimensions? - iphone

I have an uiimage with dimensions (0,0,320,460)
How to crop this image to a dimension (10,30,300,300)

Target size in my code is always set to the full screen size of the device (so you have to change it).
#implementation UIImage (Extras)
#pragma mark -
#pragma mark Scale and crop image
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
{
scaleFactor = widthFactor; // scale to fit height
}
else
{
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
{
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
{
NSLog(#"could not scale image");
}
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}

Related

How to call the Image function With in UIImagePickerViewController

How to call this function with in UIImagePickerviewcontroller
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
{
scaleFactor = widthFactor; // scale to fit height
}
else
{
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
{
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
{
NSLog(#"could not scale image");
}
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
img = [self imageByCropping:img toRect:CGRectMake(5, 5, 50, 80)]; // pass size as you need.
/// Here you got cropped image
img = [self imageByScalingAndCroppingForSize:CGSizeMake(170, 180);] // pass size as you need;
UIImageView *imgSetAlarmTime = [[UIImageView alloc] init];
imgSetAlarmTime.frame = CGRectMake(10, 50, 100, 100);
[imgSetAlarmTime setImage: img];
[self.view addSubview: imgSetAlarmTime];
}

UIImageView not resizing when bounds or frame is changed?

So I have a custom view (its a custom textview that sits on top of keyboard); however it can resize so I would like the background image (UIImageView with UIImage inside of it) to scale with it. The following code does nothing:
//size of entire custom view
CGRect bFrame = self.keyboard.frame;
bFrame.origin.y += heightDifference;
bFrame.size.height += heightDifference;
//lets set frame and bounds for the uiimageview
self.keyboard.background.frame = bFrame;
self.keyboard.background.bounds = bFrame;
//I thought the lines above would work, but they didn't, trying to reset the image and change its content mode as a hack.. .still no beans.
self.keyboard.background.image = [UIImage imageNamed:#"keyboard_backgroundv1_5.png"];
self.keyboard.background.contentMode = UIViewContentModeScaleToFill;
This function may help you
CGSize targetSize = CGSizeMake(self.keyboard.frame.size.width, self.keyboard.frame.size.height);
self.keyboard.background.image = [self imageByScalingProportionallyToSize:targetSize ImageForConvert:[UIImage imageNamed:#"keyboard_backgroundv1_5.png"]];
-(UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize ImageForConvert:(UIImage *)sourceImage
{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor*1;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage ;
}

How to show black empty space in CGContext for iOS

I have an image that is 640x480 pixels and I need to crop and center it into a 596x596 px UIImage. Any empty space should be black (it should be black above and below the image). Right now i'm cropping it like this...
-(UIImage*)cropImage:(UIImage *)theImage toFitSize:(CGSize)theSize
{
CGFloat CROP_X = floorf((theImage.size.width-theSize.width)/2);
CGFloat CROP_Y = floorf((theImage.size.height-theSize.height)/2);
CGRect imageRect = CGRectMake(CROP_X, CROP_Y, theSize.width, theSize.height);
UIGraphicsBeginImageContext(imageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGRect drawRect = CGRectMake(-imageRect.origin.x, -imageRect.origin.y, theImage.size.width, theImage.size.height);
CGContextClipToRect(context, CGRectMake(0, 0, imageRect.size.width, imageRect.size.height));
[theImage drawInRect:drawRect];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
And I also tried
- (UIImage *)croppedImage:(CGRect)bounds
{
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
But the empty space comes out as transparent. How do i do this?
Thanks
Do you really need to alter the image? If you're just going to present the clipped image, you can configure the UIImageView backgroundColor property to get the desired effect.
CGRect largerRect = CGRectMake(/* larger rect */);
CGRect smallerRect = CGRectMake(/* smaller rect */);
UIImage *croppedImage = [self cropImage:largerImage toFitSize:smallerRect];
// make the view the original size
UIImageView *imageView = [[UIImageView alloc] initWithFrame:largerRect];
imageView.image = croppedImage;
// center the cropped image and give it a loud background color
imageView.contentMode = UIViewContentModeCenter;
imageView.backgroundColor = [UIColor redColor];
this is resize image working code you only use resizeimage function and image is resize as you want width height
-`-(UIImage *)resizeImage:(UIImage *)image{
float width = image.size.width;
float height = image.size.height;
float maxSide = 310;
if (width >= height && width > maxSide)
{
width = maxSide;
height = (height*(width/image.size.width));
}
else{
if (height > maxSide)
{
height = maxSide;
width = (width * (height/image.size.height));
}
}
if ((int)width % 2 != 0)
{
width-- ;
}
if ((int)height %2 !=0)
{
height-- ;
}
UIImage *newImage;
if (width != image.size.width)
newImage = [self scaleImage:image ToSize:CGSizeMake(width,height)];
else
newImage = image;
return newImage;}- (UIImage*)scaleImage:(UIImage*)image ToSize:(CGSize)targetSize{
if (image == nil)
return nil;
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
{
}
UIGraphicsEndImageContext();
return newImage;
}
`

How can I make the large image into small with as good quality as the original image?

I am using imagview with size of 80X80 to display large image (1024 X 780).
While placing the large image into the imageview, the image looks squeezed, compressed something not like the quality one.
My question is, how can I make the large image into small with as good quality as the original image ?
In the image view, please make the imageview.mode as centre not scale to fit or any other.
I believe this would put ur any scaled image at finest quality in ur 80x80 dimension.
In this case, you should do proper Scaling of the Image that u want to show in the ImageView
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize
{
UIImage *sourceImage = chosenImage;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor*1;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage ;
}
[imageView setContentMode:UIViewContentModeScaleAspectFill];
You should however consider resizing the image and saving it on the disk if you want to use it more then once.
To resize the image:
The simplest way to resize an UIImage?
UIImage resize (Scale proportion)
UIImage: Resize, then Crop
etc..
Going from 1034x780 into 80x80 is virtually impossible to keep the quality, as there isn't enough space to capture all the details.
You can try to scale it:
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
you can set image mode [imageView setContentMode:UIViewContentModeScaleAspectFill];
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
[imageView setClipsToBounds:YES];

A thin whiteline is been added when resize the image

When we resizing the image (after downloading and before storing that in document directory), by the following code:
-(UIImage *)resizeImage:(UIImage *)image withSize:(CGSize)newSize
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = newSize.width/newSize.height;
if(imgRatio!=maxRatio){
if(imgRatio < maxRatio){
imgRatio = newSize.width / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = newSize.width;
}
else{
imgRatio = newSize.height / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = newSize.height;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[resizedImage release];
return [resizedImage autorelease];
}
this produce a re sized image with the thin white line added towards it's orientation(as if image is landscape white line is added to it's bottom and if image is portrait white line is added to it's right hand).
please tell that, how to get rid of that white line?
Thank you.
Although the size is specified in floating point units, the actual image is always an integral number of pixels.
When you calculate the new size to preserve the aspect ratio, you will typically have only one of the sides as a whole number of pixels, while the other scales to have some fractional part. When you then draw the old image into that rect, it doesn't quite fill the new image. So what you see as a white line is the graphics system's way of rendering the pixels that are part image, part background.
In essence, what you want to do is not quite possible, so you need to fudge it somehow. There are several possibilities:
Scale the image such that the aspect ratio is not perfectly preserved but you have integral values, for example by rounding:
actualWidth = round(imgRatio * actualWidth);
Maintain the aspect ratio but clip the fractional edge. The easiest way to do this is probably to make the image context a little smaller:
UIGraphicsBeginImageContext(CGSizeMake(floor(actualWidth), floor(actualHeight)));
[image drawInRect:rect];
Just fill the background first with some colour that's less obvious than white. This is a dreadful kludge, obviously, but could be effective in the right circumstances, for example if you're always drawing the image against a black background.
On a separate note, you can't call anything after return, so your final release line isn't doing anything. This is just as well because the image returned from UIGraphicsGetImageFromCurrentImageContext is autoreleased -- you should not be releasing it anyway.
This code will fix your problem:
+ (UIImage *)scaleImageProportionally:(UIImage *)image {
if (MAX(image.size.height, image.size.width) <= DEFAULT_PHOTO_MAX_SIZE) {
return image;
}
else {
CGFloat targetWidth = 0;
CGFloat targetHeight = 0;
if (image.size.height > image.size.width) {
CGFloat ratio = image.size.height / image.size.width;
targetHeight = DEFAULT_PHOTO_MAX_SIZE;
targetWidth = roundf(DEFAULT_PHOTO_MAX_SIZE/ ratio);
}
else {
CGFloat ratio = image.size.width / image.size.height;
targetWidth = DEFAULT_PHOTO_MAX_SIZE;
targetHeight = roundf(DEFAULT_PHOTO_MAX_SIZE/ ratio);
}
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
targetWidth = targetSize.width;
targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if (!CGSizeEqualToSize(imageSize, targetSize)) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = roundf(width * scaleFactor);
scaledHeight = roundf(height * scaleFactor);
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (newImage == nil) NSLog(#"could not scale image");
return newImage;
}
}