使用图像文件的一部分创建UIImageView [英] create UIImageView with portion of image file

查看:121
本文介绍了使用图像文件的一部分创建UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在继承 UIImageView 来创建基于图块的应用程序。基本上,我正在拍摄单个图像文件并将其分解为多个部分,然后将这些部分分配给我的图块( UIImageViews ),以便可以单独操作它们。 / p>

获取图像的一部分并使用它来绘制 UIImageView 的最佳方法是什么?我考虑重写 drawRect 并使用 CGAffineTransform ,但似乎应该有一种更简单的方法来做到这一点,也许通过指定一个 CGRect UIImage 传递给 UIImageView ,但我没有看到这个API。

解决方案

我们走了:

  UIImage * img = [UIImage imageNamed:@myImage.jpg]; 
CGRect imgFrame = CGRectMake(x,y,tileWidth,tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage],imgFrame);
UIImage * subImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);


I'm subclassing UIImageView to create a tile-based application. Essentially, I am taking a single image file and breaking it up into pieces, and then assigning the pieces to my tiles (UIImageViews), so that they can be manipulated independently.

What's the best way to grab a portion of an image and use that to draw a UIImageView? I thought about overriding drawRect and using CGAffineTransform, but it seems like there ought to be a simpler way to do this, perhaps by specifying a CGRect to the UIImage that is passed to the UIImageView, but I don't see an API for this.

解决方案

Here we go:

UIImage* img = [UIImage imageNamed:@"myImage.jpg"];
CGRect imgFrame = CGRectMake(x, y, tileWidth, tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], imgFrame);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);

这篇关于使用图像文件的一部分创建UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆