2016-02-08 48 views
1

我发现我们的应用程序在MKMapView上旋转注释的几个算法,并且在每个算法中图像的像素密度都会崩溃。这里是例子,绿色箭头标注: Green arrow annotation example旋转时的低像素密度UIImage

Algorithm

也许有人知道算法或延期进行图像的正确旋转?

回答

1

而不是UIGraphicsBeginImageContext,使用UIGraphicsBeginImageContextWithOptions,最后一个参数为零。这将最大化屏幕分辨率以匹配您设备的屏幕(例如视网膜)。


即使当您创建视网膜分辨率图像时,当您旋转位图时,您将会引入一些进一步的像素化。

@interface CustomUserAnnotationView : MKAnnotationView 

@property (nonatomic) CGFloat angle; 
@property (nonatomic) CGFloat lineWidth; 
@property (nonatomic, strong) UIColor *strokeColor; 
@property (nonatomic, strong) UIColor *fillColor; 

@end 

@implementation CustomUserAnnotationView 

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 

    if (self) { 
     self.lineWidth = 2; 
     self.strokeColor = [UIColor lightGrayColor]; 
     self.fillColor = [UIColor greenColor]; 
     self.backgroundColor = [UIColor clearColor]; 
     self.frame = CGRectMake(0, 0, 50, 50);    
    } 

    return self; 
} 

// you should probably `setNeedsDisplay` on the other properties' setters, too, but I'm assuming angle is the only one we're worried about right now. 

- (void)setAngle:(CGFloat)angle { 
    _angle = angle; 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect { 
    CGPoint center = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0); 
    CGFloat radius = (MIN(self.bounds.size.width, self.bounds.size.height) - self.lineWidth)/2.0; 

    UIBezierPath *path = [UIBezierPath bezierPath]; 
    [path moveToPoint: [self pointAtRadius:radius  percentAngle:0.0 center:center angleOffset:self.angle]]; 
    [path addLineToPoint:[self pointAtRadius:radius  percentAngle:0.4 center:center angleOffset:self.angle]]; 
    [path addLineToPoint:[self pointAtRadius:radius * 0.6 percentAngle:0.5 center:center angleOffset:self.angle]]; 
    [path addLineToPoint:[self pointAtRadius:radius  percentAngle:0.6 center:center angleOffset:self.angle]]; 
    [path closePath]; 
    path.lineWidth = self.lineWidth; 
    path.lineJoinStyle = kCGLineJoinRound; 

    [self.fillColor setFill]; 
    [path fill]; 

    [self.strokeColor setStroke]; 
    [path stroke]; 
} 

- (CGPoint)pointAtRadius:(CGFloat)radius percentAngle:(CGFloat)percentAngle center:(CGPoint)center angleOffset:(CGFloat)angleOffset { 
    CGFloat angle = M_PI * 2.0 * percentAngle + angleOffset; 
    return CGPointMake(center.x + radius * sin(angle), center.y - radius * cos(angle)); 
} 

@end 

enter image description here

+0

是的,它的工作原理)感谢) –

+0

仅供参考,您可能仍然遭受一些像素化:如果您绘制自己(例如,在注释视图的子类的drawRect),你可能会得到更清晰的效果,但它不会那么糟糕。您可以使用更高分辨率的图像使其更好。或者创建一个“UIBezierPath”,以任何适合您需要的旋转方式绘制该图像。但是,如果上述情况足够好,那很好。 – Rob