iPhone:-simplest way to crop image of desired shape and size - iphone

i am trying to crop image according to my desire....for my cinemagram type app.....and use it in my application
i have tried a lot of options but none of tham is...usefull for me
i read answers on stack over flow but of no use
plzz help me out
image = [UIImage imageNamed:#"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
CGSize size = [image size];
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
thanks

Here is the exact code to resize your image to desired size
CGSize itemSize = CGSizeMake(320,480); // give any size you want to give
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[myimage drawInRect:imageRect];
myimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Now my image is of your desired size

Please find the code below to the crop the image.
// Create the image from a png file
UIImage *image = [UIImage imageNamed:#"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Get size of current image
CGSize size = [image size];
// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
// Create rectangle that represents a cropped image
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4 ,
(size.width / 2), (size.height / 2));
// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];
[imageView release];
Referred from How to Crop an Image

I think this is the most simple way:
CGRect rect = CGRectMake(0.0, 0.0, 320.0, 430.0);
CGImageRef tempImage = CGImageCreateWithImageInRect([originalImage CGImage], rect);
UIImage *newImage = [UIImage imageWithCGImage:tempImage];
CGImageRelease(tempImage);
But don't forget to clear imageOrientation BEFORE cropping, otherwise you will get rotated image after cropping:
-(UIImage *)normalizeImage:(UIImage *)raw {
if (raw.imageOrientation == UIImageOrientationUp) return raw;
UIGraphicsBeginImageContextWithOptions(raw.size, NO, raw.scale);
[raw drawInRect:(CGRect){0, 0, raw.size}];
UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}

Here is a class using Dinesh's answer to get a square thumbnail of an image
class func returnSquareCroppedImage(theImage:UIImage)->UIImage{
var mySize:CGFloat = 100.0
var imageRect:CGRect
if theImage.size.width > theImage.size.height {
mySize = theImage.size.height
let xAdjust = 0-((theImage.size.width - theImage.size.height)/2)
imageRect = CGRectMake(xAdjust, 0, theImage.size.width, theImage.size.height)
}else{
mySize = theImage.size.width
let yAdjust = 0-((theImage.size.height - theImage.size.width)/2)
imageRect = CGRectMake(0, yAdjust, theImage.size.width, theImage.size.height)
}
UIGraphicsBeginImageContext(CGSizeMake(mySize, mySize))
theImage.drawInRect(imageRect)
let returnImage = UIGraphicsGetImageFromCurrentImageContext()
return returnImage
}

Related

Splitting an UIImage in two different UIimageviews in CoreGraphics-iPhone

Problem: I have an three UIImageViews. One has an UIImage and the other two are namely - leftImageView & rightImageView. The leftImageView has the left half of the image and the right one has the other. I am trying to achieve this using Coregraphics, i.e. drawing an image in the two imageviews.
But the right imageview isn't showing up. Refer to the code below:
UIImage *image = imgView.image;
CGSize sz = [image size];
CGRect leftRect = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGRect rightRect = CGRectMake(imgView.frame.size.width/2, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGImageRef leftReference = CGImageCreateWithImageInRect([image CGImage], leftRect);
CGImageRef rightReference = CGImageCreateWithImageInRect([image CGImage], rightRect);
// Left Image ...
UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, leftRect,flip(leftReference));
imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
[self.view addSubview:imgViewLeft];
// Right Image ...
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rightRect.size.width, rightRect.size.height), NO, 0);
con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, rightRect,flip(rightReference));
imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
[self.view addSubview:imgViewRight];
I think that you can use the following code part.
UIImage *image = [UIImage imageNamed:#"xxx.png"]; // You can change this line.
BOOL isLeft = YES; // You can change this variable(isLeft = YES : left piece, NO : right piece).
UIGraphicsBeginImageContext(CGSizeMake(image.size.width / 2, image.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
if (isLeft)
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
else
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(- image.size.width / 2, 0, image.size.width, image.size.height), image.CGImage);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
I couldn't launch this code now, but I think that my code is almost correct. Please try this.
Edit :
In your code :
// Right Image ...
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rightRect.size.width, rightRect.size.height), NO, 0);
con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, rightRect,flip(rightReference));
imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
There is a wrong part in the third line :
CGContextDrawImage(con, rightRect,flip(rightReference));
It should be
CGContextDrawImage(con, leftRect,flip(rightReference));

Rotated images having jagged borders even after adding transparent border

In my app I need to rotate images.I rotated the images but they have jagged borders.I googled and found that adding transparent border solves the problem, but the borders are still jagged.
My code is
-(void)CreateRotatedImage:(UIImage *)image Number:(int)Number
{
UIImage *ImageBackground=[UIImage imageNamed:#"white-highlight.png"];
CGSize ImageBackgroundSize = CGSizeMake(60, 50);
UIGraphicsBeginImageContext(ImageBackgroundSize);
CGRect ImageBackgroundRect = CGRectMake(0.0, 0.0, ImageBackgroundSize.width, ImageBackgroundSize.height);
[ImageBackground drawInRect:ImageBackgroundRect];
ImageBackground = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize ImageSize = CGSizeMake(50, 40);
UIGraphicsBeginImageContext(ImageSize);
CGRect ImageRect = CGRectMake(0.0, 0.0, ImageSize.width, ImageSize.height);
[image drawInRect:ImageRect];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(CGSizeMake(60,50));
[ImageBackground drawAtPoint:CGPointMake(0,0)];
[image drawAtPoint:CGPointMake(5,5)];
// MyscrollView.backgroundColor = [UIColor clearColor];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *pin=[UIImage imageNamed:#"pin.png"];
UIGraphicsBeginImageContext(CGSizeMake(60,50));
[newImage drawAtPoint:CGPointMake(0,0)];
[pin drawAtPoint:CGPointMake(newImage.size.height/2,0)];
// MyscrollView.backgroundColor = [UIColor clearColor];
UIImage *IntermediateImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect imageRrect = CGRectMake(0, 0, IntermediateImage.size.width, IntermediateImage.size.height);
UIGraphicsBeginImageContext( imageRrect.size );
[IntermediateImage drawInRect:CGRectMake(2,2,IntermediateImage.size.width-2,IntermediateImage.size.height-2)];
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize iSize = CGSizeMake(110, 70);
UIGraphicsBeginImageContext(iSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES);
CGContextRotateCTM (context, radians(5));
[i drawAtPoint:CGPointMake(0, 0)];
UIImage *RotatedImage=UIGraphicsGetImageFromCurrentImageContext();
//RotatedImage=[RotatedImage resizedImage:iSize interpolationQuality:kCGInterpolationHigh];
}

How can I dynamically set the size of the imageView according to the size of the image

I user Interface Builder to layout my view and image view. I put a imageView on the view and set it as an outlet. The problem is that no matter what size of image (768*1024、1024*700) I set in my program:
self.imageView.image = [UIImage imageNamed:self.filename];
The size to be on the screen is always the size of the imageView I set in the Interface Builder. How can I dynamically set the size of the imageView according to the size of the image? Thanks!
Something like:
UIImageView *imageView = [[UIImageView alloc] init];
UIImage *image = [UIImage imageNamed:#"image.png"];
CGSize imageSize = [image size];
imageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
UIImage *image = [UIImage imageNamed:self.filename];
CGFloat width = image.size.width;
CGFloat height = image.size.height;
self.imageView.frame = CGRectMake(x, y , width, height);
You could subclass your image and do the following override for eachTime a new image is set :
#interface MyImageSubClass : UIImageView;
#end
#implementation MyImageSubClass
- (void)setImage:(UIImage *)image {
// Let the original UIImageView parent class do its job
[super image];
// Now set the frame according to the image size
// and keeping the original position of your image frame
if (image) {
CGRect r = self.frame;
CGSize imageSize = [image size];
self.frame = (r.origin.x, r.origin.y, imageSize.width, imageSize.height);
}
}
#end
UIImage *buttonImage2=[UIImage imageNamed:#"blank_button_blue.png"];
oButton=[[UIButton alloc] init];
// [oButton setImage:buttonImage2 forState:UIControlStateNormal];
[oButton setFrame:CGRectMake(0, 0, buttonImage2.size.width, buttonImage2.size.height)];
UIImage* sourceImage = [UIImage imageNamed:self.filename];;
CGRect rect;
rect.size.width = CGImageGetWidth(sourceImage.CGImage);
rect.size.height = CGImageGetHeight(sourceImage.CGImage);
[ yourimageview setframe:rect];

UIImageView round corner has white background

The rounded corner has white background.
I followed other SO answers but don't know why i'm getting this whites
Bellow is the code.
UIView* testView = [[[UIView alloc] initWithFrame: self.animationView.bounds] autorelease];
UIImageView* testImageView = [[[UIImageView alloc] initWithImage:backImage] autorelease];
[testView addSubview: testImageView];
testImageView.backgroundColor = [UIColor clearColor];
CALayer* layer = [testView layer];
bool prev = layer.masksToBounds;
layer.masksToBounds = YES;
layer.cornerRadius = 30;
testView.clipsToBounds = YES;
UIImage* image = [UIImage captureView: testView];
//this image has the white regions in the four corners.
// when seen on iphone photo album
+ (UIImage*)captureView:(UIView*)view
{
CGSize size = view.bounds.size;
CGContextRef context = CreateARGBBitmapContext(size);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[view.layer renderInContext: context];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
return img;
}
Add [testImageView setClipsToBounds:YES].
It might be that you have to set testImageView.opaque = NO.

why some UIimages don't show up in iphone

hi I am currently developing a small app on ios 4.3 , using objective c
as part of the app I need to manipulate an Image that I have downloaded from the web.
the following code shows up a missing image:
(the original is in a class but I just put this together as a test scenario so that it could be easily copy pasted)
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadImage:#"http://www.night-net.net/images/ms/microsoft_vista_home_basic.jpg"];
[self getCroped:CGRectMake(10, 50, 80, 160)];
[self getCroped:CGRectMake(90, 50, 80, 80)];
[self getCroped:CGRectMake(90, 130, 40, 80)];
[self getCroped:CGRectMake(130, 130, 40, 40)];
[self getCroped:CGRectMake(130, 170, 40, 40)];
}
-(void) loadImage : (NSString*) url
{
_data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: url]];
}
-(UIImageView*) getCroped:(CGRect) imageSize{
UIImage *temp = [[UIImage alloc] initWithData:_data];
UIImage *myImage = [self resizedImage:temp and:CGSizeMake(160,160) interpolationQuality:kCGInterpolationHigh];
UIImage *image = [self croppedImage:myImage and:imageSize];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = imageSize;
[[self view] addSubview:imageView];
return imageView;
}
- (UIImage *)croppedImage:(UIImage*) image and: (CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
- (UIImage *)resizedImage:(UIImage*) image and:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
BOOL drawTransposed = NO;
return [self resizedImage:image
and:newSize
transform:[self transformForOrientation:newSize]
drawTransposed:drawTransposed
interpolationQuality:quality];
}
// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(UIImage*) image and:(CGSize)newSize
transform:(CGAffineTransform)transform
drawTransposed:(BOOL)transpose
interpolationQuality:(CGInterpolationQuality)quality {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
CGImageRef imageRef = image.CGImage;
// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(imageRef),
0,
CGImageGetColorSpace(imageRef),
CGImageGetBitmapInfo(imageRef));
// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, transform);
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, quality);
// Draw into the context; this scales the image
CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(newImageRef);
return newImage;
}
// Returns an affine transform that takes into account the image orientation when drawing a scaled image
- (CGAffineTransform)transformForOrientation:(CGSize)newSize {
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, newSize.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
return transform;
}
at first I thought this is caused by a lack of memory, but I have tested for that and that doesnt seem to be the problem,thanks in advance ofir
I've had issues in the past with images not appearing within UIWebViews if they contain unicode characters in the filename. I wonder if this might be the same thing. Try renaming your image?
doing this should be possible and low on memory cost as I did the same test,using flash to create an iphone app that does the same thing, and it works.
but I would much prefer using objective c so the question still stands