2012-11-21 70 views
0

我看過thisthis但它並沒有明確表示我的疑問。drawRect在iPhone中反轉imageView(模擬器)

我有一個自定義的imageView類,基本上是一個子類。我需要在特定像素值的UIView上添加子圖像視圖。當我使用自定義圖像查看(繪製矩形)繪製這些圖像時,我的圖像被反轉。

請幫助我在我的框架中需要做什麼改變,以便圖像不倒置。

以下是圖片。 inverted

CustomImageView.m

@implementation CustomImageView 
@synthesize customImage; 

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
     // Initialization code. 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(context, rect, customImage.CGImage); 
    [super drawRect:rect]; 
} 

- (void)dealloc { 
    [customImage release]; 
    [super dealloc]; 
} 


@end 

CustomImageView.h

@interface CustomImageView : UIView 
{ 

} 
@property(nonatomic,retain)UIImage *customImage; 
@end 

我使用這下面

-(void)drawArrowImagewithStartX:(CGFloat)X1 startY:(CGFloat)Y1 andEndX:(CGFloat)X2  endY:(CGFloat)Y2 
{ 
    CustomImageView *arrowImageView = [[CustomImageView alloc]  initWithFrame:CGRectZero]; 
    CGRect startPointFrame = CGRectZero; 
    CGRect endPointFrame = CGRectZero; 
    CGFloat arrowWidth = 0.0; 
    CGFloat arrowHeight = 0.0; 

    CGFloat y1 = -(Y1+194.0); 
    CGFloat y2 = -(Y1+194.0); 
    if (isMapZoomed) { 
     startPointFrame = CGRectMake(X1, Y1, 16.0, 16.0); 
     endPointFrame = CGRectMake(X2, Y2, 16.0, 16.0); 
     arrowWidth = 5.0; 
     arrowHeight = 25.0; 
    } 
    else { 
     startPointFrame = CGRectMake(X1, y1, 8.0, 8.0); 
     endPointFrame = CGRectMake(X2, y2, 8.0, 8.0); 
     arrowWidth = 10.0; 
     arrowHeight = 50.0; 
    } 

    CustomImageView *startImageView = [[CustomImageView alloc]  initWithFrame:CGRectZero]; 
    startImageView.frame = startPointFrame; 
    if (stepNo == 0) 
     startImageView.customImage = [UIImage imageNamed:KStartFlag]; 
    else 
     startImageView.customImage = [UIImage imageNamed:KStartPoint]; 

    CustomImageView *endImageView = [[CustomImageView alloc] initWithFrame:CGRectZero]; 
    endImageView.frame = endPointFrame; 
    if (stepNo == ([self.allPOIDetailsArray count]-1)) 
     endImageView.customImage = [UIImage imageNamed:KEndFlag]; 
    else 
     endImageView.customImage = [UIImage imageNamed:KEndPoint]; 

    if(X1 == X2){ 
     if (y1 > y2){ 
      arrowImageView.frame = CGRectMake(X2, (y1-(y1-y2)/2), arrowWidth,  arrowHeight); 
      arrowImageView.customImage =[UIImage imageNamed:KArrowUp]; 
     } 
     else{ 
      arrowImageView.frame = CGRectMake(X1, (y1+(y2-y1)/2), 5.0, 25.0); 
      arrowImageView.customImage =[UIImage imageNamed:KArrowDown]; 
      } 
    } 
    else { 
     if (X1 > X2) { 
      arrowImageView.frame = CGRectMake((X1-(X1-X2)/2), y2, 25.0, 5.0); 
      arrowImageView.customImage = [UIImage imageNamed:KArrowLeft]; 
     } 
     else{ 
      arrowImageView.frame = CGRectMake((X1+(X2-X1)/2), y1, 25.0, 5.0); 
      arrowImageView.customImage = [UIImage imageNamed:KArrowRight]; 
     } 
    } 

    for(UIView *subvw in self.zoomingView.subviews) 
     if ([subvw isKindOfClass:[UIImageView class]]) { 
      for (UIView *subview in subvw.subviews) 
       [subview removeFromSuperview]; 
     [subvw addSubview:startImageView]; 
     [subvw addSubview:endImageView]; 
     [subvw addSubview:arrowImageView]; 
     NSLog(@"Subview frame %@",NSStringFromCGRect(subvw.frame)); 
     NSLog(@"\n\nImage frame %@ %@",NSStringFromCGRect(arrowImageView.frame),NSStringFromCGRect(startImageView.frame)); 
    } 
[arrowImageView release]; 
[startImageView release]; 
[endImageView release]; 
} 

回答

0

兩全其美,使用的UIImage的drawAtPoint:drawInRect:同時還指定您的自定義背景:

UIGraphicsPushContext(context); 
[image drawAtPoint:CGPointZero]; 
UIGraphicsPopContext(); 

你也無效修改您的上下文與CGContextTranslateCTMCGContextScaleCTM

2

是的,它是正常行爲。

UIKit中的左上角座標(0,0)
的OpenGL/CoreGraphics中BOTTOMLEFT座標(0,0)

因此烏爾圖像出現反轉。

你可以解決這個問題:

  • 通過視圖的高度運動的起源了。
  • 將y軸取反(乘以-1) 。
+0

這兩種不同的方式還是一種有兩個步驟的方式? – Vjlakshmi

+0

都要執行。 – samfisher

+0

我正在使用UIKit,我得到像素值參考topleft ..所以我不需要做任何改變嗎? – Vjlakshmi