1

我正在开发一个自定义进度条,它将根据百分比值显示图像的一部分。我的想法是有两个图像,原始和黑白版本。我正在显示黑白,除此之外,我正在根据百分比裁剪原始图像。工作得很好,但从上到下。

请问,有什么办法可以让原图从下往上“长”?

以下是我到目前为止的代码示例:

    UIImageView *imgDefViewGray = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, 60)];
    imgDefViewGray.image = [UIImage imageNamed:@"castle-gray.png"];
    [self addSubview:imgDefViewGray];

    UIImageView *imgDefView = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, 60)];
    imgDefView.image = [UIImage imageNamed:@"castle.png"];

    CGRect rect = CGRectMake(0, 0 , 60, **50**);
    CGImageRef imageRef = CGImageCreateWithImageInRect([imgDefView.image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef];

    imgDefView = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, **50**)];
    imgDefView.image = img;
    [self addSubview:imgDefView];

因此,通过更改粗体(双星号)的值,我已经实现了尽可能多地裁剪原始图像,但方向为顶部->底部。我想要它底部->顶部。

提前致谢

4

1 回答 1

1

经过多次尝试,我已经想出得到我想要的结果。以下是我对我的问题的解决方案。我希望将来遇到或想要同样情况的人发现它很有用。

    UIImageView *imgDefViewGray = [[UIImageView alloc] initWithFrame:CGRectMake(170, 85, 60, 60)];
    imgDefViewGray.image = [UIImage imageNamed:@"castle-gray.png"];//60x60 b&w image

    UIImage originalImg = [UIImage imageNamed:@"castle.png"];//60x60 original image
    CGRect rect = CGRectMake(0, originalImg.size.height-scaleDefence), 60 , scaleDefence);//scaleDefence is just a float variable that converts current percentage value to px.

    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImg CGImage], rect);
    UIImage *croppedImg = [UIImage imageWithCGImage:imageRef];

    UIImageView *imgDefView = [[UIImageView alloc] initWithFrame:CGRectMake(0, originalImg.size.height-scaleDefence, 60, scaleDefence)];//dynamically changing y-axis depending on cropped image's height.
    imgDefView.image = croppedImg;

    [imgDefViewGray addSubview:imgDefView];//make original image part of b&w
    [self addSubview:imgDefViewGray];//finally add the result to the view
于 2012-12-14T21:14:43.833 回答