2019独角兽企业重金招聘Python工程师标准>>>
初始化:
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