2012-11-27 57 views
0

如果我覆盖drawRect为了显示图像并在其上放置一个由dinamically生成的覆盖图(请参阅代码),每当放大图像时,都会以非常模糊的方式绘制它,因为缩放。drawRect大小的两倍

图像是由两部分组成的,一张从png中画出的图像(其原始大小是想要的2倍,所以它不应该给出缩放时的问题,但它确实),另一种是根据矩形大小,所以它也应该适应当前的矩形大小,但它不适用。

任何帮助?

- (void) drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGContextDrawImage(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height), [UIImage imageNamed:@"actionBg.png"].CGImage); 

    // generate the overlay 
    if ([self isActive] == NO && self.fullDelay != 0) { // TODO: remove fullDelay check! 
     UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0); 
     CGContextRef overlayCtx = UIGraphicsGetCurrentContext(); 

     int segmentSize = (rect.size.height/[self fullDelay]); 

     for (int i=0; i<[self fullDelay]; i++) { 
      float alpha = 0.9 - (([self fullDelay] * 0.1) - (i * 0.1)); 
      [[UIColor colorWithRed:120.0/255.0 green:14.0/255.0 blue:14.0/255.0 alpha:alpha] setFill]; 

      if (currentDelay > i) { 
       CGRect r = CGRectMake(0, i * segmentSize, rect.size.width, segmentSize); 
       CGContextFillRect(overlayCtx, r); 
      } 
      [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3] setFill]; 
      CGRect line = CGRectMake(0, (i * segmentSize) + segmentSize - 1 , rect.size.width, 1); 
      CGContextFillRect(overlayCtx, line); 
     } 

     UIImage *overlay = UIGraphicsGetImageFromCurrentImageContext(); 
     UIImage *overlayMasked = [TDUtilities maskImage:overlay withMask:[UIImage imageNamed:@"actionMask.png"]]; 

     // prevent the drawings to be flipped 
     CGContextTranslateCTM(overlayCtx, 0, rect.size.height); 
     CGContextScaleCTM(overlayCtx, 1.0, -1.0); 

     CGContextSetBlendMode(ctx, kCGBlendModeMultiply); 
     CGContextDrawImage(ctx, rect, overlayMasked.CGImage); 
     CGContextSetBlendMode(ctx, kCGBlendModeNormal); 

     UIGraphicsEndImageContext(); 
    } 

回答

0

的问题是,你是画overlayMaskedCGContextDrawImage一个CGImage,它什么都不知道的规模。如果你处于双比例的情况,你必须自己手动加倍大小,否则你应该使用UIImage,它知道比例。