调整视图大小时,UIBezier曲线变得模糊

时间:2014-08-15 22:48:10

标签: objective-c uiimageview scale blur uibezierpath

我有一个集合视图,其用户可以更改其单元格大小。集合视图的模板单元格在右下角包含UIImageView。此图像视图与集合视图单元按比例缩放。在此图像视图中,我使用UIBezierPath对象在不同时间执行一些自定义绘制。

如果我绘制图像并且用户更改单元格大小,则图像按比例缩放和插值,变得稍微模糊。好像图像被缓存和重复使用,但这不是我想要的。我希望每个单元格必须在其图像视图中重绘其曲线,以使它们变得清晰而不模糊。

以下是我在UIImageView中绘图的示例:

//ImageView.m
//Draw a custom image (forgive the magic numbers)
-(void)drawImage{
    UIGraphicsBeginImageContext(self.bounds.size);
    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(1, 1, self.bounds.size.width-2, self.bounds.size.height-2)];
    [[UIColor whiteColor] setStroke];
    [circle setLineWidth:self.bounds.size.width/44.0f];
    [[UIColor redColor] setFill];
    [circle fill];
    [circle stroke];

    UIBezierPath *checkmark = [UIBezierPath bezierPath];
    float r = self.bounds.size.width / 160.0f;
    [checkmark moveToPoint:CGPointMake(40.0f*r, 40.0f*r)];
    [checkmark addLineToPoint:CGPointMake(120.0f*r,120.0f*r)];
    [checkmark moveToPoint:CGPointMake(40.0f*r, 120.0f*r)];
    [checkmark addLineToPoint:CGPointMake(120.0f*r, 40.0f*r)];
    [checkmark setLineWidth:self.bounds.size.width/22.0f];
    [checkmark stroke];
    [self setImage:UIGraphicsGetImageFromCurrentImageContext()];
    UIGraphicsEndImageContext();
}

以下是我如何更改单元格大小:

//ViewController.m 
//The user chooses a cell size from a UISegmentedControl.
-(IBAction)selectedSegmentIndexChanged:(UISegmentedControl *)sender{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    int i = sender.selectedSegmentIndex;
    int p = 2.0f*(4-i);
    int s = (320-p*i)/(i+1);
    layout.itemSize = CGSizeMake(s,s);
    layout.minimumInteritemSpacing = p;
    layout.minimumLineSpacing = p;
    [self.collectionView setCollectionViewLayout:layout animated:YES];
}

1 个答案:

答案 0 :(得分:0)

我最终使用通知来实现重绘图像的目标。

布局完成后,我会发布通知:

//ViewController.m 
//The user chooses a cell size from a UISegmentedControl.
-(IBAction)selectedSegmentIndexChanged:(UISegmentedControl *)sender{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    int i = sender.selectedSegmentIndex;
    int p = 2.0f*(4-i);
    int s = (320-p*i)/(i+1);
    layout.itemSize = CGSizeMake(s,s);
    layout.minimumInteritemSpacing = p;
    layout.minimumLineSpacing = p;
    [self.collectionView setCollectionViewLayout:layout 
                                        animated:YES 
                                      completion:^(BOOL finished{
                                          [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionViewLayoutDidChange" object:nil];
                                      }];
}

imageView会观察通知并重新绘制:

//ImageView.m
//
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setContentMode:UIViewContentModeScaleAspectFit];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:@"collectionViewLayoutDidChange" object:nil];
}

//I also added the following method prototype to ImageView.h
-(void)didReceiveNotification:(NSNotification *)notification{
    if ([notification.name isEqualToString:@"collectionViewLayoutDidChange"]){
        [self drawImage];
        return;
    }
}