2012-10-25 40 views
0

我知道这听起来像是一个非常基本的任务,但我试过搜索等,但没有结果负载的UIImage

例如这里就是我试图做

的例子加载图像

Image Map

向图像映射到的UIImage

请求从图像的x,y,宽度和高度。

例如:

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

只是作为一个例子。如果我,例如,传递{64,0,64,64}作为RECT那么结果将是:

UIImage Result

任何建议? :)

问候

蒂姆·埃利斯

回答

0

当然只要你发布你在其他地方找到了答案......

对于那些希望我在这里找到

Cropping an UIImage

我使用的代码基本相同,但我将它添加到UIImage中以使事情变得容易

这对于那些有兴趣

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

主文件

//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 

立即致电它很容易。

下面是一个示例:)

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];