Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lynkynpark86

macrumors 6502
Original poster
I have a UIImage set by a UIImagePickerController, but the image needs to be exactly 115x115. How would I detect if the image is square, and if it is, scale it up or down to 115x115, and if it isn't square, crop it to get a square (but include most of the image), then scale it down to 115x115? I've been looking around online, and I can't find much.
 
When I was looking for it, google was full of it, that's 2 weeks ago.
This is my code

Code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage* image = (UIImage* ) [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    imageFromCamera = [image copy];
    
    // Save image
    UIImageWriteToSavedPhotosAlbum(imageFromCamera, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    [picker release];
    
    CGSize size = CGSizeMake(340, 480);
    UIGraphicsBeginImageContext(size);// a CGSize that has the size you want
    [imageFromCamera drawInRect:CGRectMake(0,0,size.width,size.height)];
    //image is the original UIImage
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
    imageData = UIImageJPEGRepresentation(newImage, 0.5);
    imageDataTotal = [imageData copy];
}

Code:
    UIImage *imageFromCamera;
    NSData *imageData;
    NSData *imageDataTotal;

I got no time to explain, but i'm sure u can handle it.
 
To check of the size of the returned UIImage, use its size property. From that you can do any calculation to create a Rect that will be used to return a square representation.

I haven't used the image picker controller much, but you may want to try the following to get back a square of your image. I'll leave it to you read the documentation and integrate as needed. Combined with jnoxx's post, this should get the result you're looking for. You do this before the drawInRect: method.

I believe jnoxx's code assumes it is being run on the main thread, as UIGraphicsBeginImageContext suggest that to me.


Code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* image = (UIImage* ) [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Calculate a new rect for the pixels of interest. Assuming a 960x720 image from an iPod touch camera.
// The horizontal middle pixels.
CGRect sqrRect = CGRectMake(120,0,720,720);

// A retained pointer because the word Create in this function indicates that. We'll release below.
CGImageRef squareCGImage = CGImageCreateWithImageInRect( image.CGImage, sqrRect);

// An auto released UIImage via class method, so no need to release.
// Can skip the need for a UIImage if you use CGContextDrawImage().
UIImage* squareUIImage = [UIImage imageWithCGImage: squareCGImage]; 

// Release after assignment to UIImage or use in CGContextDrawImage(), and last use.
CGImageRelease(squareCGImage);

...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.