English 中文(简体)
为什么CASHAPELAYER 太大 尽管设置内容规模?
原标题:Why is CAShapeLayer too big despite setting contentsScale?

我有一个CASHAPELAYer, 我用它来绘制条形图中的小点。 问题在于这些点在视网膜显示器上都太大了。 我已经设置了内容比例表, 并检查了它是否已经设置了 。 但我还是看不出它与它有什么区别!

CAShapeLayer *lineShapeLayer = [CAShapeLayer layer];
lineShapeLayer.contentsScale = [[UIScreen mainScreen] scale]; 
CGMutablePathRef path = CGPathCreateMutable();
lineShapeLayer.backgroundColor = [UIColor clearColor].CGColor;
lineShapeLayer.bounds = CGRectMake(0, 0, self.backgroundLayer.bounds.size.width, 1.5);

lineShapeLayer.anchorPoint = CGPointMake(0, 0);
lineShapeLayer.position = CGPointMake(0, rint((self.verticalPartitionHeight)*i));
lineShapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1], [NSNumber numberWithInt:2], nil];
lineShapeLayer.strokeColor = [UIColor blackColor].CGColor;
lineShapeLayer.lineWidth = 1;
NSLog(@"contentsScale: %f", lineShapeLayer.contentsScale);

CGPathMoveToPoint(path, nil, 0, .5);       
CGPathAddLineToPoint(path, nil, lineShapeLayer.frame.size.width, .5);
[lineShapeLayer setPath:path];

[self.backgroundLayer addSublayer:lineShapeLayer];

Here is the view if I make it with PNGs: PNGs showing 1px dots

Here is the view with the CAShapeLayer code (set to line width .5 even.): CAShapeLayer showing 4px dots :(

最佳回答

您的代码“ 坚固” 没有什么错, 除非它位于别处 < / 坚固 > 。 我用 < code> 本身在环形( Im 假设 < code> < i 是索引) 内运行上面的代码 。 垂直partitionH8 = 3. 0

下面的两张图像是正常分辨率和视网膜分辨率的结果。 我放大了普通图像, 使其大小相同。 正如您可以看到的, 点在两个分辨率上的大小( 点, 不是像素) 相同 。

正常分辨率,扩大两次

""https://i.sstatic.net/N1ADd.png" alt="Retina决议"/ >

Edit:

您提到储量应用程序在 Landscape 中这样做, 但如果您放大了它的截图, 您将会看到它实际上使用点( 因为每个点是 2x2 像素) 。 您可以在下面的截图( 从一个视网膜截图) 中看到这一点, 我放大了很多点, 并选择了一个点的像素 。

的预期行为 。

“https://i.sstatic.net/IJR0M.png' alt=“stocks.ap dots”/> https://i.sstatic.net/IJR0M.png' alt=“stocks.app dots”/>

Edit 2:

再放大一下您的点。 它们实际上是“ 坚固”, 与苹果股票应用程序中的点的大小( / 坚固) 完全相同。 您可以在下面的图像中看到它, 我在那里选择了 < em> one < / em > 像素 。

""https://i.sstatic.net/J3SqX.png" alt="此处输入图像描述"/ >

问题回答

@David Ronnqvist 是对的! 内容比例表是有效的。 我只需要把线宽缩小一点,

    CAShapeLayer *lineShapeLayer = [CAShapeLayer layer];
    lineShapeLayer.contentsScale=[[UIScreen mainScreen] scale]; 
    CGMutablePathRef path = CGPathCreateMutable();
    lineShapeLayer.backgroundColor=[UIColor clearColor].CGColor;
    lineShapeLayer.bounds=CGRectMake(0, 0, self.backgroundLayer.bounds.size.width, 0.5);

    lineShapeLayer.anchorPoint=P(0,0);
    lineShapeLayer.position=P(0,rint((self.verticalPartitionHeight)*i));
    lineShapeLayer.lineDashPattern=[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.5], [NSNumber numberWithInt:1], nil];
    lineShapeLayer.strokeColor=[UIColor grayColor].CGColor;
    lineShapeLayer.lineWidth=0.5;
    lineShapeLayer.shadowColor=[UIColor whiteColor].CGColor;
    lineShapeLayer.shadowOffset=CGSizeMake(0, 0.5);
    lineShapeLayer.shadowOpacity=1.0;
    lineShapeLayer.shadowRadius=0;


    NSLog(@"lineShapeLayer.frame: %@", NSStringFromCGRect(lineShapeLayer.frame));
    CGPathMoveToPoint(path, nil, 0, 0.25);       
    CGPathAddLineToPoint(path, nil, lineShapeLayer.frame.size.width, 0.25);
    [lineShapeLayer setPath:path];

当然,我会做这个不是刻度的硬码, 而是缩度的缩放。

"https://i.sstatic.net/Sh3CL.png" alt="看起来完美"/ >

缩放 CAShapeLayer 的最佳方法如下:

CAShapeLayer *pathLayer = [CAShapeLayer layer];   
[pathLayer setTransform:CATransform3DMakeScale(0.1, 0.1, 0.1)];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签