您的位置:首页 > 产品设计 > UI/UE

UIImage与UIImageView有神马关系?

2013-11-27 16:11 579 查看
UIImage与UIImageView关系:

1:让我们看一看他们之间的关系:

UIImage 继承自NSObject,而不是继承自UIView,UIResponder,也就是说,UIImage并不能够接受事件,并且也是无法在UI上面直接显示的。

那么怎么去显示一个UIImage呢?当然需要一个用来盛放UIimage的容器,并且这个容器必须继承自UIView。

------

UIView可以装图片么?

没有方法。

UILabel继承自UIView,它能设置图片么?

答案是:也不行。

UIButton继承自UIview,它为什么可以设置图片?

- (void)setImage:(UIImage *)image forState:(UIControlState)state; 

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state; 
我们看看UIButton的成员变量:

CFMutableDictionaryRef _contentLookup;
    UIEdgeInsets           _contentEdgeInsets;
    UIEdgeInsets           _titleEdgeInsets;
    UIEdgeInsets           _imageEdgeInsets;
    UIImageView           *_backgroundView;
    UIImageView           *_imageView;
    UILabel               *_titleView;
    BOOL                  _initialized;
可以看出,Button之所以能够设置图片,只是因为在button中内嵌了UIImageView;

所以:

我们可以得出结论,在IOS中,UIimageView是能够盛放UIimage的唯一容器。在其他容器中只有添加UIImageView子视图才能显示图片(补充,还可以使用UIImage的drawInrect将它Draw到一个UIview中去)。

2:UIImageView对UIImage做了哪些工作?

从UIImageView的官方文档中似乎得不到任何东西,因为它只有

除了两个构造函数:

- (id)initWithImage:(UIImage *)image;
- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage
默认不能进行交互

@property(nonatomic,getter=isUserInteractionEnabled)BOOL
userInteractionEnabled;       
// default is NO

还有几个自带的用来生成动画的方法:

@property(nonatomic,copy)NSArray
*animationImages;           
// The array must contain UIImages. Setting hides the single image. default is nil
@property(nonatomic,copy)
NSArray *highlightedAnimationImages
NS_AVAILABLE_IOS(3_0);           //
The array must contain UIImages. Setting hides the single image. default is nil

@property(nonatomic)NSTimeInterval
animationDuration;        
// for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
@property(nonatomic)NSInteger      animationRepeatCount;     //
0 means infinite (default is 0)

- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;

似乎没有任何其它与UIimage有关的属性==。==
好吧:当我没说,所有的操作都被封装到构造方法中了。

3:关于UIImage
沿着文档阅读,首先是UIImageOrientation:

这玩意的主要作用是控制image的绘制方向,共有以下8中方向:


其效果就像用仿射变换进行各种各样的旋转(实际上自然不一样)

    UIImageOrientationUp,            // default orientation
    UIImageOrientationDown,          // 180 deg rotation
    UIImageOrientationLeft,          // 90 deg CCW
    UIImageOrientationRight,         // 90 deg CW
下面是镜面效果:
    UIImageOrientationUpMirrored,   
// as above but image mirrored along other axis. horizontal flip
    UIImageOrientationDownMirrored,  // horizontal flip
    UIImageOrientationLeftMirrored,  // vertical flip
    UIImageOrientationRightMirrored, // vertical flip

那么这玩意怎么使用,是不是有某个方法可以直接改变绘制image的方式呢?

通过搜索,得到下面几个可能相关的构造函数:

加方法:

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientationNS_AVAILABLE_IOS(4_0);
+ (UIImage *)imageWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientationNS_AVAILABLE_IOS(6_0);
减方法:

- (id)initWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientationNS_AVAILABLE_IOS(4_0);
- (id)initWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientationNS_AVAILABLE_IOS(6_0);
------------------------------------------------------
利用这些方法,可以在初始化图片的时刻对图片方向进行旋转。可是难道不是初始化时就不能改变方向了?

通过这个属性貌似不行,但是可参考下面的哥们的解决方法:
http://www.cnblogs.com/smileEvday/archive/2013/05/14/UIImage.html
大体就是说:会不断重新绘制新的UIImage,而参数就是当前图片的CGImage和新的旋转方向。

他用的就是

- (id)initWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientationNS_AVAILABLE_IOS(6_0);

--------------------------------------

继续往下看:

又是个枚举,你他么逗我?UIImageResizingMode是什么玩意。
其实,它就是用来对图片进行拉伸或者压缩的。

+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)durationNS_AVAILABLE_IOS(6_0);
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingModeNS_AVAILABLE_IOS(6_0); 
很抽象的样子,到底用来干什么?
参考:http://blog.csdn.net/q199109106q/article/details/8615661
例如:有一张图片想要被拉伸,例如现在聊天系统中的起泡,你不可能给每个气泡做一张图吧,可是如果做一张图片,让它随着父类比例随意拉伸的话,这玩意肯定会丑爆了!可是如果不让四个角做拉伸,只让中间区域做拉伸,那么这张图片不就不会变形了?是不是很叼?
IOS5.0:

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsetsNS_AVAILABLE_IOS(5_0);
返回一个只在特定区域拉伸的UIImage。
IOS6.0:



- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); 

比5.0版本多了一个参数,也就是我们提到的UIImageResizingMode:(终于讲到点上了,好激动)
这个枚举,只有两个值:

UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片

UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片

关于:animatedResizableImageNamed这个加方法:

从名字上面来看就是好像是动态改变图片。

官方说明:

// animated images. When set as UIImageView.image, animation will play in an infinite loop until removed. Drawing will render the first image
这里弄了半天,才弄出来了一点效果:

+ (UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)durationNS_AVAILABLE_IOS(5_0); //
read sequence of files with suffix starting at 0 or 1

生成一个动画image对象。这里的name只需要传入一个前缀,如image,就会读取目录下所有以image开头的png图片作为一个动画,图片间可以少一些图片,例如就算没有image3,image4和后面的image也会被读取,这个动画不需要手动去启动。(但是图片的尺寸必须一致,否则读取不到)

+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets duration:(NSTimeInterval)durationNS_AVAILABLE_IOS(5_0);//
sequence of files

+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)durationNS_AVAILABLE_IOS(6_0);
同样,只不过当被加入到UIImageView中时,如果UIimgaeView的尺寸大于图片的话, 决定图片的拉伸区域和方式。

+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)durationNS_AVAILABLE_IOS(5_0);
这四个方法均是+构造函数,会生成一个UIimage对象。而这个image的内容就相当于一个gif对象(并不是)。
只不过,这个方法中与UIImageview的动画不同的是,所有格式必须是png结尾,尺寸和scale必须相同。

未完,翻页----》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIImage UIImageView