码迷,mamicode.com
首页 > 移动开发 > 详细

iOS学习-UIImageView

时间:2016-01-20 22:29:31      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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
/*******************************************************/

技术分享

iOS学习-UIImageView

标签:

原文地址:http://www.cnblogs.com/MrWuYindi/p/5146730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!