0

I know this sounds like a pretty basic task but I've tried searching and such but with no result

For example here's an example of what I'm attempting to do

Load Image

Image Map

Into an image map into UIImage

Request image from x,y with a width and height.

eg:

-(UIImage *) loadImgFromImageMap:(UIImage *)map withRect:(CGRect)dimensions;

just as an example. and if I, for example, pass {64, 0, 64, 64} as the rect then the result would be:

UIImage Result

Any suggestions? :)

Regards

Tim Ellis

Tim
  • 522
  • 1
  • 5
  • 18

1 Answers1

0

Of course as soon as you post you find the answer elsewhere...

For those looking I found it here

Cropping an UIImage

The code I used is basically the same but I add it to UIImage to make things easy

Here it is for those interested

//The .h file
@interface UIImage (crop)
- (UIImage *)crop:(CGRect)rect;
@end

Main File

//The .m file
@implementation UIImage (crop)

- (UIImage *)crop:(CGRect)rect {
    if (self.scale > 1.0f) {
        rect = CGRectMake(rect.origin.x * self.scale,
                          rect.origin.y * self.scale,
                          rect.size.width * self.scale,
                          rect.size.height * self.scale);
    }

    CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}

@end

Calling it very easy now.

Here's a sample :)

UIImage *image = [UIImage imageNamed:@"imgMap.png"];
CGRect frame = CGRectMake (0, 0, 40, 40); //Change this to whatever the frame should be
UIImage *cropped = [image crop:frame];
Community
  • 1
  • 1
Tim
  • 522
  • 1
  • 5
  • 18