【发布时间】: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