1

image

这是我设置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;}
Vinodh
  • 5,262
  • 4
  • 38
  • 68
代思思
  • 13
  • 5
  • 20 x 20 is clearly too small for the bitmap you are drawing. Replace CGSizeMake(20, 20) with the appropriate size. – Clafou Jan 24 '18 at 13:15
  • thank you . Even if I set a large size, the picture is blurred – 代思思 Jan 24 '18 at 13:53
  • I guess the issue here is about actual image size for retina screen, which most should consider the screen "scale" factor. Check out this one: https://stackoverflow.com/a/13487677/1142802 And, actually, as you are using single color for that, I would suggest that you may use a view with layer.cornerRadius, which suppose to be more efficient than generating UIImage object. – Hail Zhang Jan 26 '18 at 08:49
  • 创建Bitmap图形上下文的方法 方法1 UIGraphicsBeginImageContext(<#CGSize size#>); 方法2 UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 使用两个方法同样都可以创建,但是使用第一个方法将来创建的图片清晰度和质量没有第二种方法的好。 – 代思思 Jan 26 '18 at 13:24

1 Answers1

1
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;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Chandresh Kachariya
  • 667
  • 2
  • 13
  • 31