Reputation: 13
这是我设置slider的thumbImage
[_slider setThumbImage:[UIImage getRoundImageWithColor:SharedColor.themeGreen size:CGSizeMake(20, 20)] forState:UIControlStateNormal];
This is the thumbImage that is drawn。I am in order to show the video playback progress bar
+ (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;}
Upvotes: 1
Views: 171
Reputation: 677
UIImage *image = [[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
OR
UIImage *image = [self imageWithColor:[UIColor redColor]];
Set Image in a slider.
[slider setThumbImage:[self imageWithImage:image scaledToSize:CGSizeMake(32, 59)] forState:UIControlStateNormal];
This is the thumbImage that is drawn in the slider.
- (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;
}
Pass UIColor and create an image if we have not any image.
- (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;
}
Upvotes: 1