初始化:
UIImageView *_imageView = [[UIImageView alloc] init];
//其他初始化
/*
initWithFrame:<#(CGRect)#>
initWithCoder:<#(nonnull NSCoder *)#>
initWithImage:<#(nullable UIImage *)#>
initWithImage:<#(nullable UIImage *)#> highlightedImage:<#(nullable UIImage *)#>
*/
显示方式:居中、左、右,填充、拉伸、适应,类似设置屏保
_imageView.contentMode = UIViewContentModeScaleToFill;
//其他方式
/*
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
*/
自动布局:
_imageView.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
/*
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
*/
循环播放图片:
UIImage *image1 = [UIImage imageNamed:@"1"];
UIImage *image2 = [UIImage imageNamed:@"2"];
UIImage *image3 = [UIImage imageNamed:@"3"];
NSArray *imagesArray = @[image1,image2,image3];
_imageView.animationImages = imagesArray;
_imageView.animationDuration = [imagesArray count];
//循环次数
_imageView.animationRepeatCount = 0;
[_imageView startAnimating];
加载图片:
UIImage *image1 = [UIImage imageNamed:@"1"];
UIImage *image2 = [UIImage imageWithContentsOfFile:@"filePath"];
UIImage *image3 = [UIImage imageWithData:<#(nonnull NSData *)#>];
//image1、image2 会在内存中做缓存,图片多了消耗内存,但同一张图片只缓存一次,可重复利用。
//image3 以数据方式加载,展示网络图片,或者存储数据库展示图片,
实例:选择图片
- (void)viewDidLoad
{
[super viewDidLoad];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubview:_imageView];
_imageView.backgroundColor = [UIColor redColor];
//
_imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(choosePictrue)];
//tap.numberOfTapsRequired = 2;
[_imageView addGestureRecognizer:tap];
[tap release];
[_imageView release];
}
- (void)choosePictrue{
//调用相机拍照片或者直接获取相册中的照片
UIImagePickerController *picc = [[UIImagePickerController alloc] init];
picc.delegate = self;
picc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picc animated:YES completion:nil];
[picc release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
_imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end
如何理解图片下载?
一般使用懒加载和图片缓存的模式,懒加载就是需要的时候才去临时加载。节省内存空间和用户流量。缓存是把url地址做成md5存在沙盒中以便下次直接使用。
SDWebImage的原理?
一个基于NSOperation/NSInvokeOperation的一个多线程图片缓存框架。每次通过[uiimageview setimageWithUrl:]就启动一个异步线程,负责下载网络图片。一旦下载完成就把网址使用md5做一个校验,然后存在沙盒管理目录中,以便下次直接从沙盒中读取。而不需从网络上下载,节省了流量,加快图片访问速度。
如果界⾯比较卡怎么办
- 图⽚要⽤用异步下载,并且要缓存起来
- 对于图⽚的加载到界⾯面上 要复⽤ 为什么要用异步? 异步的主要是为了界⾯面流畅
http://blog.csdn.net/wave_1102/article/details/49913825
网友评论