iOS-评分图片处理(可以小数)

效果图.png
[self setStar:3.7];

- (void)setStar:(CGFloat)num{
    NSString *nu = [NSString stringWithFormat:@"%.1f",num];
    NSArray *array = [nu componentsSeparatedByString:@"."];
    NSInteger number = [array[0] integerValue];;
    CGFloat point = [array[1] integerValue]/10.0;
    for (int i = 0; i < 5; i ++) {
        UIImageView *image = [[UIImageView alloc]init];
        if (i < number) {
            image.image = IMGNAME(@"星");
        } else{
            image.image = IMGNAME(@"星 (1)");
        }
        if (i == number && point != 0) {
            image = [ZQTools cutImage:IMGNAME(@"星") fraction:point];
        }
        [self.viewWithAppraise addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            MLEFT(13 * i);
            make.centerY.mas_equalTo(0);
            MHEIGHT(11);
            MWIDTH(11);
        }];
    }
}

图片处理。思路是把红色图片截取,然后截取出的图片放到imageView上。imageView的width = 图片image.width * 相应的小数,其他参数相同。再用一个imageView放背景图,两个imageView重叠。

+ (UIImageView *)cutImage:(UIImage *)image fraction:(CGFloat)fractonPart{
    
    CGFloat width = image.size.width * fractonPart * image.scale;
    CGRect newFrame = CGRectMake(0, 0, width , image.size.height * image.scale);
    CGImageRef resultImage = CGImageCreateWithImageInRect(image.CGImage, newFrame);
    UIImage *result = [UIImage imageWithCGImage:resultImage scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(resultImage);
    UIImageView *imageNo = [[UIImageView alloc]init];
    imageNo.frame = CGRectMake(0, 0, ADAPTATION_WIDTH(11), ADAPTATION_HEIGHT(11));//ADAPTATION_WIDTH、HEIGHT宽高自适应。
    imageNo.image = IMGNAME(@"星 (1)");
    UIImageView *imageNew = [[UIImageView alloc]init];
    imageNew.image = result;
    imageNew.frame = CGRectMake(0, 0, ADAPTATION_WIDTH(11 * fractonPart), ADAPTATION_HEIGHT(11));
    [imageNo addSubview:imageNew];
    return imageNo;
}

所有的星星图片加载到view上,星星图片宽高为11。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容