.h文件
typedef NS_ENUM(NSInteger, LowerList) {
Left = 1,
Right = 2
};
@interface VC_AddStoryModel : UIViewController
{
UIScrollView *scroll;
LowerList lower;
CGFloat viewsWidth;
UIView *leftView;
CGFloat leftHeight;
UIView *rightView;
CGFloat rightHeight;
BOOL firstPicture;
}
.h文件
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
{
viewsWidth = ScreenWidth / 2;
firstPicture = YES;
scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
scroll.contentSize = CGSizeMake(ScreenWidth, ScreenHeight);
scroll.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scroll];
leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewsWidth, 0)];
rightView = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH / 2, 0, viewsWidth, 0)];
[scroll addSubview:leftView];
[scroll addSubview:rightView];
scroll.backgroundColor = [UIColor yellowColor];
}
[self layoutImage];
}
- (void)layoutImage {
NSArray* urls = @[
@"http://img0.bdstatic.com/img/image/shouye/leimu/mingxing.jpg",
@"http://img1.bdstatic.com/img/image/8662934349b033b5bb5c55e5d9834d3d539b700bcce.jpg",
@"http://img.baidu.com/img/image/3bf33a87e950352a5947ae485143fbf2b2.jpg",
@"http://imgstatic.baidu.com/img/image/7af40ad162d9f2d3cdc19be8abec8a136227cce1.jpg",
@"http://imgstatic.baidu.com/img/image/weimeiyijing0207.jpg",
@"http://e.hiphotos.baidu.com/image/w%3D400/sign=2e56c8010ed79123e0e095749d355917/ae51f3deb48f8c5470385d2638292df5e1fe7fd4.jpg",
@"http://c.hiphotos.baidu.com/image/w%3D400/sign=e37cc47c6509c93d07f20ff7af3cf8bb/7a899e510fb30f2468cc6271ca95d143ad4b0369.jpg",
@"http://b.hiphotos.baidu.com/image/w%3D400/sign=ac0b8e2b92ef76c6d0d2fa2bad17fdf6/a71ea8d3fd1f4134dedc5974271f95cad0c85ed4.jpg",
@"http://imgstatic.baidu.com/img/image/huacaozhiwu0207.jpg",
@"http://d.hiphotos.baidu.com/image/w%3D400/sign=7d27c75af4246b607b0eb374dbf81a35/5882b2b7d0a20cf4f28367d674094b36acaf99ac.jpg",
];
dispatch_queue_t queue = dispatch_queue_create("get image", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i < 100; i++) {
__block UIImage *image;
@synchronized(scroll) {
dispatch_async(queue, ^{
NSString *imageUrl = urls[i % 10];
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
image = [UIImage imageWithData:data];
});
dispatch_async(queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self makePictuerVisible:image];
});
});
}
}
}
- (void)makePictuerVisible:(UIImage *)image {
lower = [self checkList];
CGFloat width = viewsWidth;
CGFloat height = width * image.size.height / image.size.width;
UIImageView *picture = nil;
switch (lower) {
case Left:
{
picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, leftView.frame.size.height, width, height)];
leftView.frame = CGRectMake(leftView.frame.origin.x, leftView.frame.origin.y, viewsWidth, leftView.frame.size.height + height);
[leftView addSubview:picture];
}
break;
case Right:
picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, rightView.frame.size.height, width, height)];
rightView.frame = CGRectMake(rightView.frame.origin.x, rightView.frame.origin.y, viewsWidth, rightView.frame.size.height + height);
[rightView addSubview:picture];
break;
default:
break;
}
picture.image = image;
if (Left == [self checkList]) {
[scroll setContentSize:CGSizeMake(ScreenWidth, rightView.frame.size.height + 1)];
} else {
[scroll setContentSize:CGSizeMake(ScreenWidth, leftView.frame.size.height + 1)];
}
NSLog(@"%f %f",scroll.contentSize.height, scroll.contentSize.width);
}
- (LowerList)checkList {
LowerList temp;
if (firstPicture) {
temp = Left;
firstPicture = NO;
} else {
leftHeight = leftView.frame.size.height;
rightHeight = rightView.frame.size.height;
if (leftHeight > rightHeight) {
temp = Right;
} else {
temp = Left;
}
}
return temp;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
实现的效果是顺序把图片添加进界面