Image Cropping issue - iphone

I want to crop UIImage with the following code:
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
// or use the UIImage wherever you like
UIImage * img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
This code is working fine in the simulator but giving unusual result on device.

Create a UIImage category and try adding this.
#implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)cropRect {
cropRect = CGRectMake(cropRect.origin.x*self.scale,
cropRect.origin.y*self.scale,
cropRect.size.width*self.scale,
cropRect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}

Try this one :
- (UIImage *)cropImage:(UIImage *)oldImage {
CGSize imageSize = oldImage.size;
UIGraphicsBeginImageContextWithOptions(CGSizeMake( imageSize.width,imageSize.height - 150),NO,0.);
[oldImage drawAtPoint:CGPointMake( 0, -80) blendMode:kCGBlendModeCopy alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}

Related

How to crop the image in objective c?

The user can change the cropbox size which is shows default in edit screen. I tried with below code :
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect {
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
But it cropped fixed area. How to crop area which is selected by user ?
For Get Crop Image:
UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake("AS YOu Need"); //set your rect size.
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
Use following code for call croppIngimageByImageName:toRect: method that return UIImage (with specific size of image)
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
CGRect clippedRect = CGRectMake(0 ,0,180 ,180);
CGImageRef imageRef = CGImageCreateWithImageInRect(imgVw1.image.CGImage, clippedRect);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
imgVw1Cliped.image=newImage;
NSLog(#"%d",imgVw1Cliped.image.imageOrientation);

Break down a picture in xcode for an iphone app

Is there any way that I can have the user upload an image into the app, say for example a 50X150 pixels image, and I can break it into 3 50x50 pixel images?
If so, can someone help me to select certain pixels and break it into several images?
Thank you!
Use this code...
// In following method inRect:(CGRect)rect >>> this rect should be 50x50 or you can define according to your requirements..
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:image.imageOrientation];
CGImageRelease(newImageRef);
return newImage;
}
For more visit this reference..
Hope, this will help you...enjoy
Define a category on UIImage that gives you a great cropping method:
- (UIImage *)cropImageInRect:(CGRect)cropRect
{
CGImageRef image = CGImageCreateWithImageInRect(self.CGImage,cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
return croppedImage;
}
Now with this category you can easily do what you want to:
UIImage *original = ...;
UIImage left = [original cropImageInRect:CGRectMake(0.0, 0.0, 50.0, 50.0)];
UIImage center = [original cropImageInRect:CGRectMake(0.0, 50.0, 50.0, 50.0)];
UIImage right = [original cropImageInRect:CGRectMake(0.0, 100.0, 50.0, 50.0)];
I needed this, too. Added to a utils category method on UIImage:
// UIImage+Utls.h
#interface UIImage (UIImage_Utls)
- (UIImage *)subimageInRect:(CGRect)rect;
- (NSArray *)subimagesHorizontally:(NSInteger)count;
#end
// UIImage+Utls.m
#import "UIImage+Utls.h"
#implementation UIImage (UIImage_Utls)
- (UIImage *)subimageInRect:(CGRect)rect {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *answer = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return answer;
}
- (NSArray *)subimagesHorizontally:(NSInteger)count {
NSMutableArray *answer = [NSMutableArray arrayWithCapacity:count];
CGFloat width = self.size.width / count;
CGRect rect = CGRectMake(0.0, 0.0, width, self.size.height);
for (int i=0; i<count; i++) {
[answer addObject:[self subimageInRect:rect]];
rect = CGRectOffset(rect, width, 0.0);
}
return [NSArray arrayWithArray:answer];
}
#end

Resize UIImage programmatically iphone

I want to crop a UIImage that the size of the UIImage is 640*960 and i want to crop it and it will be 640*640.
I try to use this method:
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);]]
and this CGRect : (0,0,640,640)
but it give me UIImage that is not 640*640 from the original UIImage
use the following function
UIImage *newImage = [self imageWithImage:mainDelegate.starImage scaledToSize:CGSizeMake(640, 640)];
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This will return the image with size (newSize.width,newSize.height)

cropping an UIImage to a new aspect ratio

i want to crop an UIImage to get a new aspect ratio...
bounds is a CGRect with (0,0, newwidth, newhigh)...
- (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect(myImage.CGImage, bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGSize asd = croppedImage.size;
return croppedImage;
}
with the call:
[workImage croppedImage: workImage: CGRectMake(0, 0, newWidth, newHeigh)];
after that the "workimage" have the same size as before...
what could be wrong?
regards
Well, you aren't altering the current image as this seems to be a category method on UIImage. You are creating a new image and returning it. So what will work is this,
workImage = [workImage croppedImage: workImage: CGRectMake(0, 0, newWidth, newHeigh)];
However I think the method is better named like this, (assuming it as a category method on UIImage)
- (UIImage *)croppedImageWithRect:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGSize asd = croppedImage.size;
return croppedImage;
}
This way you will call it like this,
workImage = [workImage croppedImageWithRect:CGRectMake(0, 0, newWidth, newHeigh)];
And as a side note, don't use methods like croppedImage::. It is better to name all parameters like say croppedImage:rect:.

Simple resizing image

I've been trying to use a method commonly used to resize an image. Without using this method, here is the code that takes a url of an image.
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image = img;
This works fine. But when I try to use this method:
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
and call it using this:
UIImage *scaledImage = [self imageWithImage:img scaledToSize:CGSizeMake(10.0f,10.0f)];
then putting into my table like this:
cell.imageView.image = scaledImage;
Nothing shows up. Is there something I'm missing here?
This is similar to Exporting customized UITableViewCells into UIImage
Here's what you need to do in your -imageWithImage:scaledToSize: method, modified from my answer to that question:
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContextForScaledImage = CGBitmapContextCreate(nil, newSize.width, newSize.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
// Draw the image's layer into the context.
UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
[imageView.layer renderInContext:bitmapContextForCell];
[imageView release];
// Create a CGImage from the context.
CGImageRef cgScaledImage = CGBitmapContextCreateImage(bitmapContextForScaledImage);
// Create a UIImage from the CGImage.
UIImage * scaledImage = [UIImage imageWithCGImage:cgScaledImage];
// Clean up.
CGImageRelease(cgScaledImage);
CGContextRelease(bitmapContextForScaledImage);
return scaledImage;
}