这是我设置滑块的thumbImage
[_slider setThumbImage:[UIImage getRoundImageWithColor:SharedColor.themeGreen size:CGSizeMake(20, 20)] forState:UIControlStateNormal];
这是绘制的thumbImage。我是为了显示视频播放进度条
+ (UIImage *)getRoundImageWithColor:(UIColor*)color size:(CGSize)size{
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillEllipseInRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;}
答案 0 :(得分:0)
UIImage *image = [[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
OR
UIImage *image = [self imageWithColor:[UIColor redColor]];
在滑块中设置图像。
[slider setThumbImage:[self imageWithImage:image scaledToSize:CGSizeMake(32, 59)] forState:UIControlStateNormal];
这是在滑块中绘制的thumbImage。
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
如果我们没有任何图像,请传递UIColor并创建图像。
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 32.0f, 32.0f);
UIGraphicsBeginImageContextWithOptions(rect.size, CGColorGetAlpha(color.CGColor) == 1.0f , 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}