zoukankan      html  css  js  c++  java
  • UIImageView的属性contentMode

    UIImageView的属性contentMode

      实现网络图片根据UIImageView的大小来等比例缩放显示。

      苹果自带的有设置UIImageView的contentMode属性,

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

    注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。 UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView 会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部 分图片显示出来。

    我们也可下载图片获取图片的原始宽高,然后根据比例重新设置图片的大小也是可以的,不过这种的话就可能会出现问题了,先下载获取一次之后又给imageView赋值时就会出现缓慢延迟的现象,这是在赋值时又下了一遍,不赞成使用。

    //获取网络图片的宽高

    + (CGSize)getImageSizeWithURL:(NSURL *)url {

    //    1.创建CGImageSourceRef

        CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)url, NULL);

        CGFloat width = 0.0f, height = 0.0f;

        

        if (imageSource)

        {

    //        获取图像的属性信息

            CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

            

            if (imageProperties != NULL)

            {

                CFNumberRef widthNum  = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);

                if (widthNum != NULL) {

                    NSNumber *num = (__bridge NSNumber *)(widthNum);

                    width = [num floatValue];

                    //                CFNumberGetValue(widthNum, kCFNumberFloatType, &width);

                }

                

                CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);

                if (heightNum != NULL) {

                    NSNumber *num = (__bridge NSNumber *)(heightNum);

                    height = [num floatValue];

                    //                CFNumberGetValue(heightNum, kCFNumberFloatType, &height);

                }

                

                CFRelease(imageProperties);

            }

            CFRelease(imageSource);

        }

        return CGSizeMake(width, height);

    }

     第三种,就是使用SDWebImage的方法了,也可以实现吧,效果还可以

    [self.toyImgView sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

            

            NSLog(@"*****%lf",image.size.width);

            NSLog(@"******%lf",image.size.height);

            

        }];

     大家根据不同的情况使用不同的方法吧。

  • 相关阅读:
    Maidsafe-去中心化互联网白皮书
    The Top 20 Cybersecurity Startups To Watch In 2021 Based On Crunchbase
    Top 10 Blockchain Security and Smart Contract Audit Companies
    The 20 Best Cybersecurity Startups To Watch In 2020
    Blockchain In Cybersecurity: 11 Startups To Watch In 2019
    004-STM32+BC26丨260Y基本控制篇(阿里云物联网平台)-在阿里云物联网平台上一型一密动态注册设备(Android)
    涂鸦开发-单片机+涂鸦模组开发+OTA
    000-ESP32学习开发-ESP32烧录板使用说明
    03-STM32+Air724UG远程升级篇OTA(阿里云物联网平台)-STM32+Air724UG使用阿里云物联网平台OTA远程更新STM32程序
    03-STM32+Air724UG远程升级篇OTA(自建物联网平台)-STM32+Air724UG实现利用http/https远程更新STM32程序(TCP指令,单片机程序检查更新)
  • 原文地址:https://www.cnblogs.com/19940122yzc/p/5698881.html
Copyright © 2011-2022 走看看