【问题标题】:UIBezier curve becomes blurry when resizing view调整视图大小时贝塞尔曲线变得模糊
【发布时间】:2014-08-15 22:48:09
【问题描述】:

我有一个集合视图,用户可以更改其单元格大小。集合视图有一个模板单元格,其右下角包含一个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];
}

【问题讨论】:

    标签: objective-c uiimageview scale blur uibezierpath


    【解决方案1】:

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

    当布局完成动画后,我会发布一条通知:

    //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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-06
      • 2017-11-30
      • 2013-03-03
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      相关资源
      最近更新 更多