【问题标题】:Load UIImage from Image Map从图像映射加载 UIImage
【发布时间】:2012-10-15 04:47:21
【问题描述】:

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

例如,这是我正在尝试做的一个示例

加载图片

成图片映射成UIImage

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

例如:

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

仅作为示例。例如,如果我将 {64, 0, 64, 64} 作为矩形传递,那么结果将是:

有什么建议吗? :)

问候

蒂姆·埃利斯

【问题讨论】:

    标签: xcode cocoa-touch ios5 ios6


    【解决方案1】:

    当然,一旦您发布,您就会在其他地方找到答案......

    对于那些寻找我在这里找到它的人

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多