• iOS学习-UIImageView


    //
    //  ViewController.m
    //  UIImageView详解
    //
    //  Created by 大欢 on 16/1/20.
    //  Copyright © 2016年 bjsxt. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self saveImage];

    //    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg"]];
    //    UIImage * image = [UIImage imageWithData:data];
    //    UIImageView * imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    //    imageView.image = image;
    //    [self.view addSubview:imageView];
    }



    - (void)saveImage {
        
        NSString * path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"data"];
        NSData * data = [NSData dataWithContentsOfFile:path];
        UIImage * image = [UIImage imageWithData:data];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        imageView.image = image;
        [self.view addSubview:imageView];
        
        NSString * localPath = @"/Users/dahuan/Desktop/123.data";
        //    NSData * localData = UIImagePNGRepresentation(image);
        
        //param2 图片的质量
        NSData * localData = UIImageJPEGRepresentation(image, 0.5);
        [localData writeToFile:localPath atomically:YES];
        
    }

    - (void)createImageView {
        
    //UIImageView - UIImage
    //
    //    1、imageNamed:方法
    //imageNamed:是UIImage的一个类方法,它做的事情比我们看到的要稍微多一些。它的加载流程如下:
    //    a. 系统回去检查系统缓存中是否存在该名字的图像,如果存在则直接返回。
    //    b. 如果系统缓存中不存在该名字的图像,则会先加载到缓存中,在返回该对象。
    //      观察上面的操作我们发现系统会缓存我们使用imageNamed:方法加载的图像时候,系统会自动帮我们缓存。这种机制适合于那种频繁用到界面贴图累的加载,但如果我们需要短时间内频繁的加载一些一次性的图像的话,最好不要使用这种方法。
    //
    //    2、imageWithContentsOfFile:和imageWithData:方法
    //      图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。

        UIImage * image1 = [UIImage imageNamed:@"baby.jpg"];

        NSString * path = [[NSBundle mainBundle] pathForResource:@"baby" ofType:@"jpg"];

    //    UIImage * image1 = [UIImage imageWithContentsOfFile:path];

        UIImageView * imageView = [[UIImageView alloc] initWithImage:image1];

        imageView.frame = CGRectMake(20, 20, image1.size.width, image1.size.height);
        
        [self.view addSubview:imageView];
    }

    - (void)endFile {
        
        //png,pdf不需要添加后缀名
        //jpg等添加后缀名
        
        //    UIImage * image = [UIImage imageNamed:@"fengjing"];
        //    UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
        //    imageView.frame = self.view.frame;
        //    [self.view addSubview:imageView];
        
        
    }

    - (void)contentMode {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"baby" ofType:@"jpg"];
        
        UIImage * image1 = [UIImage imageWithContentsOfFile:path];
        
        UIImageView * imageView = [[UIImageView alloc] initWithImage:image1];
        
        imageView.backgroundColor = [UIColor grayColor];
        
        imageView.frame = CGRectMake(20, 20, 100, 100);
        
        /*
         
         contentMode属性
         这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:
         
         UIViewContentModeScaleToFill
         UIViewContentModeScaleAspectFit
         UIViewContentModeScaleAspectFill
         UIViewContentModeRedraw
         UIViewContentModeCenter
         UIViewContentModeTop
         UIViewContentModeBottom
         UIViewContentModeLeft
         UIViewContentModeRight
         UIViewContentModeTopLeft
         UIViewContentModeTopRight
         UIViewContentModeBottomLeft
         UIViewContentModeBottomRight
         
         注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形,图片填充满frame(默认)。
         UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。
         
         */
        
        imageView.contentMode = UIViewContentModeLeft;
        
        //根据Bounds剪切之外的内容
        imageView.clipsToBounds = YES;
        
        [self.view addSubview:imageView];
    }
    @end
    /*******************************************************/

  • 相关阅读:
    Labeling Balls
    Following Orders
    Frame Stacking
    Window Pains
    Sort it all out
    Ferry Loading||
    今年暑假不AC
    xcode10 出现 框架 或者 pod 出错
    网络请求 步骤
    swift UIAlertController使用 UIAlertController的宽度 为270
  • 原文地址:https://www.cnblogs.com/MrWuYindi/p/5146730.html
Copyright © 2020-2023  润新知