1

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!

KTobiassen
  • 21
  • 1
  • 2

3 Answers3

2

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

Community
  • 1
  • 1
Nitin
  • 7,455
  • 2
  • 32
  • 51
  • I'm still very new to objective C and can't seem to get the syntax down. I tried using `code` UIImage *original = [info objectForKey:UIImagePickerControllerOriginalImage]; imageView.image = original; imageView1.image = [original ImageFromImage:image inRect:CGRectMake(0.0, 0.0, 50.0, 50.0)]; 'code' but I get an error saying "No visible @interface for 'UIImage' declares the selector 'ImageFromImage:InRect'" `code` Can you please lead me in the right direction for the method call and that error? Thank you! – KTobiassen Apr 22 '12 at 04:55
  • When you call method at that time you should have to pass UIImage and CGReect value...For Example call method as i shown below.... `UIImage *Image = [UIImage imageNamed:@"yourimage.png"]; CGRect *rect = CGRectMake(0.0, 0.0, 50.0, 50.0); [Self imageFromImage:image:rect];' – Nitin Apr 22 '12 at 05:03
1

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)];
Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
1

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
danh
  • 62,181
  • 10
  • 95
  • 136