1.设置自动轮播图的渐变效果:
参考我柱哥哥的博客:iOS——简单实现图片渐变
2.设置自动轮播图的渐变效果时,imageWithData
获取图片为nil:
imageWithData 使用分析
最终将获取网络请求的图片改为:
UIView *colorView = [[UIView alloc] init];
colorView.frame = CGRectMake(0, myWidth / 1.4, myWidth, myWidth - myWidth / 1.4);
UIColor *colorOne = [[[self class] mostColor:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.top_storiesImage[(self.currentIndex) % self.allIndex]]]]] colorWithAlphaComponent:1.0];
UIColor *colorTwo = [[[self class] mostColor:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.top_storiesImage[(self.currentIndex) % self.allIndex]]]]] colorWithAlphaComponent:0.0];
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
CAGradientLayer *gradient = [CAGradientLayer layer];
//设置开始和结束位置(设置渐变的方向)
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(0, 0);
gradient.colors = colors;
gradient.frame = CGRectMake(0, 0, myWidth, myWidth - myWidth / 1.4);
[colorView.layer insertSublayer:gradient atIndex:0];
[self.centerRollButton addSubview:colorView];