UIImageView基础详解

来源:互联网 发布:苏联数学 知乎 编辑:程序博客网 时间:2024/06/05 17:18

NSBundle

//束,目录NSString * string = [NSBundle mainBundle].bundlePath;NSLog(@"%@",string);//Resource:文件名//ofType:文件名后缀//inDirectory:Bundle下的目录NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];NSString * path2 = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist" inDirectory:@"image"];NSArray * array = [NSArray arrayWithContentsOfFile:path2];NSLog(@"%@",array);

Bundle(包)中的图片素材

往项目中拖拽素材时,通常选择

(1) Destination: 勾选

(2) Folders:

1)选择第一项:黄色文件夹    注意点:Xcode中分文件夹,Bundle中所有所在都在同一个文件夹下,因此,不能出现文件重名的情况    特点:   a.可以直接使用[NSBundle mainBundle]作为资源路径,效率高!   b.可以使用[UIImage imageNamed:]加载图像2)选择第二项:蓝色文件夹    注意点:Xcode中分文件夹,Bundle中同样分文件夹,因此,可以出现文件重名的情况    特点:    a.需要在[NSBundle mainBundle]的基础上拼接实际的路径,效率较差    b.不能使用[UIImage imageNamed:]加载图

UIImageView详解

/*
1、imageNamed:方法
imageNamed:是UIImage的一个类方法,它做的事情比我们看到的要稍微多一些。它的加载流程如下:
a. 系统回去检查系统缓存中是否存在该名字的图像,如果存在则直接返回。
b. 如果系统缓存中不存在该名字的图像,则会先加载到缓存中,在返回该对象。
  观察上面的操作我们发现系统会缓存我们使用imageNamed:方法加载的图像时候,系统会自动帮我们缓存。这种机制适合于那种频繁用到界面贴图类的加载,但如果我们需要短时间内频繁的加载一些一次性的图像的话,最好不要使用这种方法。
*/

`#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {[super viewDidLoad];// UIImageView的剪切UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];imageView.image = [UIImage imageNamed:@"qq"];// 设置圆角// 保证切一个圈,首先要保证是个正方形,然后cornerRadius应该等于边长的一半imageView.layer.cornerRadius = CGRectGetWidth(imageView.frame) / 2.0;// YES:去掉剪切的部分 NO:不剪切imageView.layer.masksToBounds = YES;// 设置边框颜色imageView.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5].CGColor;// 设置边框大小imageView.layer.borderWidth = 0.5f;[self.view addSubview:imageView];[self contentMode];

}
`
*// 图片的显示
*

`- (void)contentMode
{

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(300, 100, self.view.frame.size.width - 40, 300)];// 超范围的view剪切imageView.clipsToBounds = YES;imageView.backgroundColor = [UIColor yellowColor];imageView.image = [UIImage imageNamed:@"baby.jpg"];/* UIViewContentModeScaleToFill 属性会导致图片变形,图片填充满frame(默认)。 UIViewContentModeScaleAspectFit 会保证图片比例不变(图片不会失真),而且全部显示在ImageView中,这意味着ImageView会有部分空白。 UIViewContentModeScaleAspectFill 也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。 */imageView.contentMode = UIViewContentModeScaleAspectFill;[self.view addSubview:imageView];

}
`

`
- (void)test
{

//  有缓存UIImage *image1 = [UIImage imageNamed:@"baby.jpg"];// 没有缓存NSString *file = [[NSBundle mainBundle] pathForResource:@"baby.jpg" ofType:nil];UIImage *image = [UIImage imageWithContentsOfFile:file];// 从网络加载图片,返回NSData// 图片的文件路径必须包含图片的后缀(.png, .jpg)//    NSData dataWithContentsOfURL:<#(nonnull NSURL *)#>// 通过文件路径加载图片,返回NSData//    NSData dataWithContentsOfFile:<#(nonnull NSString *)#>// 把NSData转化为UIImage//    UIImage imageWithData:<#(nonnull NSData *)#>// UIImage转化为NSData(文件比较大)//   NSData *data1 = UIImagePNGRepresentation(image);// 压缩系数(0-1)//   NSData *data1 = UIImageJPEGRepresentation(image, 1);UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];[self.view addSubview:imageView];

}`

`
- (void)image
{

CGFloat width = self.view.frame.size.width - 40;UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, width, 400)];// 第一次先缓存,后加载,下次掉用的时候直从缓存中加载//    imageView.image = [UIImage imageNamed:@"baby"];// 后面的方法都没有缓存// 通过file路径来加载图片/* Resource: 文件名字 ofType: 文件后缀 */NSString *file = [[NSBundle mainBundle] pathForResource:@"baby.jpg" ofType:nil];NSLog(@"file:%@", file);{    // 根据文件路径直接加载图片    UIImage *image = [UIImage imageWithContentsOfFile:file];    imageView.image = image;}//    imageView.image = image;{    // 根据文件路径返回一个二进制的图片    NSData *data = [NSData dataWithContentsOfFile:file];    // 根据二进制图片转化为UIImage    UIImage *image = [UIImage imageWithData:data];    imageView.image = image;}{    // 把图片转化为二进制    UIImage *image = [UIImage imageNamed:@"baby.jpg"];    //比较大    NSData *data = UIImagePNGRepresentation(image);    NSLog(@"data:%zi", data.length);    // compressionQuality压缩参数(0-1)    // 比较小(一般情况下都使用他)    NSData *data1 = UIImageJPEGRepresentation(image, 0.4);    NSLog(@"data1:%zi", data1.length);    NSFileManager *fileManager = [NSFileManager defaultManager];    [fileManager createFileAtPath:@"/Users/andezhou/Desktop/image.data" contents:data attributes:nil];    // 直接加载工程目录中的二进制图片    NSString *file1 = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"data"];    UIImage *image1 = [UIImage imageWithContentsOfFile:file1];    imageView.image = image1;}{    // 从网络上下载图片    NSURL *url = [NSURL URLWithString:@"http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg"];    NSData *data = [NSData dataWithContentsOfURL:url];    // 二进制NSData转化为UIImage    UIImage *image = [UIImage imageWithData:data];    imageView.image = image;}[self.view addSubview:imageView];

}
`

`
- (void)createImageView
{

// 初始化一个imageView并设置frame//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 150, 150)];// 初始化一个图片UIImage *image = [UIImage imageNamed:@"qq"];// 获取图片的sizeNSLog(@"imageSize:%@", NSStringFromCGSize(image.size));// 初始化一个imageView并设置好图片,此处自动设置好了图片size,X和Y坐标都0UIImageView *imageView = [[UIImageView alloc] initWithImage:image];imageView.backgroundColor = [UIColor clearColor];// 设置一张图片//    imageView.image = [UIImage imageNamed:@"qq"];[self.view addSubview:imageView];

}
`

图片的拉伸

`- (void)resizedImage1
{

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 400)];UIImage *image = [UIImage resizedImage:@"qipao" capX:0.15 capY:0.8];/* leftCapWidth ,是左侧需要保留的像素数 topCapHeight是顶部需要保留的像素数 被拉伸的点那1像素用于中间的平铺,达到最后所需要的尺寸。效果相当于只能保持一边固定,拉伸另一边。 并且以前使用image后,没有设置到指定的大小,所以没有看到效果。 *///    UIImage *image1 = [image stretchableImageWithLeftCapWidth:image.size.width * 0.15f topCapHeight:image.size.height * 0.8f];imageView.image = image;[self.view addSubview:imageView];

}
`
gif动画的播放

`
+ (UIImageView )imageViewWithGIFFile:(NSString )file frame:(CGRect)frame
{

UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];// 加载gif文件数据NSData *gifData = [NSData dataWithContentsOfFile:file];// GIF动画图片数组NSMutableArray *frames = nil;// 图像源引用CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)gifData, NULL);// 动画时长CGFloat animationTime = 0.f;if (src) {    // 获取gif图片的帧数    size_t count = CGImageSourceGetCount(src);    // 实例化图片数组    frames = [NSMutableArray arrayWithCapacity:count];    for (size_t i = 0; i < count; i++) {        // 获取指定帧图像        CGImageRef image = CGImageSourceCreateImageAtIndex(src, i, NULL);        // 获取GIF动画时长        NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(src, i, NULL);        NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];        NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];        animationTime += [delayTime floatValue];        if (image) {            [frames addObject:[UIImage imageWithCGImage:image]];            CGImageRelease(image);        }    }    CFRelease(src);}[imageView setImage:[frames objectAtIndex:0]];[imageView setBackgroundColor:[UIColor clearColor]];[imageView setAnimationImages:frames];[imageView setAnimationDuration:animationTime];[imageView startAnimating];return imageView;

}`

0 0
原创粉丝点击