zoukankan      html  css  js  c++  java
  • ios开发之UIImageView

    废话少说,直接进入正题!!!

    1、创建一个UIImageView:

    创建一个UIImageView对象的几种方法:

    UIImageView *imageView1 = [[UIImageView alloc] init]; 
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)]; UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)]; UIImageView *imageView4 = [[UIImageView alloc] nitWithImage:(UIImage *) highlightedImage:(UIImage *)]; UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];

    比较常用的是前边三个。至于第四个,当这个ImageView的highlighted属性是YES时,显示的就是参数highlightedImage,一般情况下显示的是第一个参数UIImage。

    2、frame与bounds属性:

    上述创建一个UIImageView的方法中,第二个方法是在创建时就设定位置和大小。

    当之后想改变位置时,可以重新设定frame属性:

    imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
    imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);

    那么bounds跟frame属性有什么区别呢?

    frame设置其位置和大小,而bounds只能设置其大小,其参数中的x、y不起作用即便是之前没有设定frame属性,控件最终的位置也不是bounds所设定的参数。bounds实现的是将UIImageView控件以原来的中心为中心进行缩放。

    来看下面的示例:

    imageView.frame = CGRectMake(0, 0, 320, 460);
    imageView.bounds = CGRectMake(100, 100, 160, 230);

    执行之后,这个imageView的位置和大小是(80, 115, 160, 230)。

    3、contentMode属性:

    这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:

    UIViewContentModeScaleToFill
    UIViewContentModeScaleAspectFit
    UIViewContentModeScaleAspectFill
    UIViewContentModeRedraw
    UIViewContentModeCenter
    UIViewContentModeTop
    UIViewContentModeBottom
    UIViewContentModeLeft
    UIViewContentModeRight
    UIViewContentModeTopLeft
    UIViewContentModeTopRight
    UIViewContentModeBottomLeft
    UIViewContentModeBottomRight

    注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。

    UIViewContentModeScaleToFill属性会导致图片变形。

    UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。

    UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。

    4、更改位置

    4.1 直接修改其frame属性

    4.2 修改其center属性:

    imageView.center = CGPointMake(CGFloat x, CGFloat y);

    center属性指的就是这个ImageView的中间点。

    4.3 使用transform属性

    imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);

    其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。

    5、旋转图像

    imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);

    注意:它是按照顺时针方向旋转的,而且旋转中心是原始ImageView的中心,也就是center属性表示的位置。

    这个方法的参数angle的单位是弧度,而不是我们最常用的度数,所以可以写一个宏定义:

    #define degreesToRadians(x) (M_PI*(x)/180.0)

    6、缩放图像

    还是使用transform属性:

    imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
    其中,CGFloat scale_w与CGFloat scale_h分别表示将原来的宽度和高度缩放到多少倍

    7、播放一系列图片

    imageView.animationImages = imagesArray;
    // 设定所有的图片在多少秒内播放完毕
    imageView.animationDuration = [imagesArray count];
    // 不重复播放多少遍,0表示无数遍
    imageView.animationRepeatCount = 0;
    // 开始播放
    [imageView startAnimating];

    8、为图片添加单击事件:

    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
    [imageView addGestureRecognizer:singleTap];

    一定要先将userInteractionEnabled置为YES,这样才能响应单击事件。

    9、其他设置

    imageView.hidden = YES或者NO;                                   // 隐藏或者显示图片
    imageView.alpha = (CGFloat) al;                                // 设置透明度
    imageView.highlightedImage = (UIImage *)hightlightedImage;     // 设置高亮时显示的图片
    imageView.image = (UIImage *)image;                            // 设置正常显示的图片
    [imageView sizeToFit];                                         // 将图片尺寸调整为与内容图片相同

    10、图片缓存(最新版SDWebImage使用)

    <1>下载SDWebImage,导入工程。github托管地址https://github.com/rs/SDWebImage

    <2>在需要的地方导入头文件

    #import "UIImageView+WebCache.h"

    <3>调用sd_setImageWithURL:方法缓存图片,注意,这就是新版本的新方法,旧方法是setImageWithURL:。

          1. sd_setImageWithURL:

    //图片缓存的基本代码,就是这么简单
    [self.image1 sd_setImageWithURL:imagePath1];

          2. sd_setImageWithURL:  completed:

    //用block 可以在图片加载完成之后做些事情
    [self.image2 sd_setImageWithURL:imagePath2 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
             
            NSLog(@"这里可以在图片加载完成之后做些事情");
             
        }];

          3. sd_setImageWithURL:  placeholderImage:

    //给一张默认图片,先使用默认图片,当图片加载完成后再替换
    [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];

          4. sd_setImageWithURL:  placeholderImage:  completed:

    //使用默认图片,而且用block 在完成后做一些事情
    [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
             
            NSLog(@"图片加载完成后做的事情");
             
        }];

          5. sd_setImageWithURL:  placeholderImage:  options:

    //options 选择方式
    [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"] options:SDWebImageRetryFailed];

    除了带options选项的方法,其他的方法都是综合存储,也就是内存缓存和磁盘缓存结合的方式,如果你只需要内存缓存,那么在options这里选择SDWebImageCacheMemoryOnly就可以了。

    到这里你已经可以用SDWebimage进行图片缓存了(over)

    /**************************************************************************************************/

    深入了解部分

    options所有的选项:

         //失败后重试
         SDWebImageRetryFailed = 1 << 0,
          
         //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
         SDWebImageLowPriority = 1 << 1,
          
         //只进行内存缓存
         SDWebImageCacheMemoryOnly = 1 << 2,
          
         //这个标志可以渐进式下载,显示的图像是逐步在下载
         SDWebImageProgressiveDownload = 1 << 3,
          
         //刷新缓存
         SDWebImageRefreshCached = 1 << 4,
          
         //后台下载
         SDWebImageContinueInBackground = 1 << 5,
          
         //NSMutableURLRequest.HTTPShouldHandleCookies = YES;
          
         SDWebImageHandleCookies = 1 << 6,
          
         //允许使用无效的SSL证书
         //SDWebImageAllowInvalidSSLCertificates = 1 << 7,
          
         //优先下载
         SDWebImageHighPriority = 1 << 8,
          
         //延迟占位符
         SDWebImageDelayPlaceholder = 1 << 9,
          
         //改变动画形象
         SDWebImageTransformAnimatedImage = 1 << 10,

    SDWebImage内部实现过程

    1. 入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImageManager 根据 URL 开始处理图片。

    2. 进入 SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交给 SDImageCache 从缓存查找图片是否已经下载 queryDiskCacheForKey:delegate:userInfo:.

    3. 先从内存图片缓存查找是否有图片,如果内存中已经有图片缓存,SDImageCacheDelegate 回调 imageCache:didFindImage:forKey:userInfo: 到 SDWebImageManager。

    4. SDWebImageManagerDelegate 回调 webImageManager:didFinishWithImage: 到 UIImageView+WebCache 等前端展示图片。

    5. 如果内存缓存中没有,生成 NSInvocationOperation 添加到队列开始从硬盘查找图片是否已经缓存。

    6. 根据 URLKey 在硬盘缓存目录下尝试读取图片文件。这一步是在 NSOperation 进行的操作,所以回主线程进行结果回调 notifyDelegate:。

    7. 如果上一操作从硬盘读取到了图片,将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)。SDImageCacheDelegate 回调 imageCache:didFindImage:forKey:userInfo:。进而回调展示图片。

    8. 如果从硬盘缓存目录读取不到图片,说明所有缓存都不存在该图片,需要下载图片,回调 imageCache:didNotFindImageForKey:userInfo:。

    9. 共享或重新生成一个下载器 SDWebImageDownloader 开始下载图片。

    10. 图片下载由 NSURLConnection 来做,实现相关 delegate 来判断图片下载中、下载完成和下载失败。

    11. connection:didReceiveData: 中利用 ImageIO 做了按图片下载进度加载效果。

    12. connectionDidFinishLoading: 数据下载完成后交给 SDWebImageDecoder 做图片解码处理。

    13. 图片解码处理在一个 NSOperationQueue 完成,不会拖慢主线程 UI。如果有需要对下载的图片进行二次处理,最好也在这里完成,效率会好很多。

    14. 在主线程 notifyDelegateOnMainThreadWithInfo: 宣告解码完成,imageDecoder:didFinishDecodingImage:userInfo: 回调给 SDWebImageDownloader。

    15. imageDownloader:didFinishWithImage: 回调给 SDWebImageManager 告知图片下载完成。

    16. 通知所有的 downloadDelegates 下载完成,回调给需要的地方展示图片。

    17. 将图片保存到 SDImageCache 中,内存缓存和硬盘缓存同时保存。写文件到硬盘也在以单独 NSInvocationOperation 完成,避免拖慢主线程。

    18. SDImageCache 在初始化的时候会注册一些消息通知,在内存警告或退到后台的时候清理内存图片缓存,应用结束的时候清理过期图片。

    19. SDWI 也提供了 UIButton+WebCache 和 MKAnnotationView+WebCache,方便使用。

    20. SDWebImagePrefetcher 可以预先下载图片,方便后续使用。

    从上面流程可以看出,当你调用setImageWithURL:方法的时候,他会自动去给你干这么多事,当你需要在某一具体时刻做事情的时候,你可以覆盖这些方法。比如在下载某个图片的过程中要响应一个事件,就覆盖这个方法:

        //覆盖方法,指哪打哪,这个方法是下载imagePath2的时候响应
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
         
        [manager downloadImageWithURL:imagePath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
             
            NSLog(@"显示当前进度");
             
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
             
            NSLog(@"下载完成");
        }];

    转载http://my.oschina.net/plumsoft/blog/76128 

          http://www.cocoachina.com/ios/20141212/10622.html

  • 相关阅读:
    java 获取最近三个月的月初和月末日期
    java 字符串与十六进制互转
    @Validated 参数校验及配置
    mybatis 存取Oracle数据库中Blob类型数据
    Java下载服务器文件到前端
    Java实现Oracle拷贝数据插入到MySQL
    保利、绿地用移动质检3.0管进度、质量,让快周转又快又好!
    未造先知,匠心协同-BIM全过程总咨询实践案例赏析-2021版
    大数据资产管理总体框架概述
    “数据资产全生命周期管理”你要知道的九大问题
  • 原文地址:https://www.cnblogs.com/20150507-smile/p/4846498.html
Copyright © 2011-2022 走看看