0

我已经看到了这个这个,但这并不能消除我的疑问。

我有一个自定义的 imageView 类,基本上是一个子类。我需要在特定像素值的 UIView 上添加子图像视图。当我使用自定义图像视图(绘制矩形)绘制这些图像时,我的图像被反转。

请帮助我需要在我的框架中进行哪些更改,以使图像不会倒置。

下面是图片。倒

自定义图像视图.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];
}
4

2 回答 2

2

是的,这是正常行为。

UIkit 左上角坐标 (0,0)
OpenGL/CoreGraphics 左下角坐标 (0,0)

因此你的图像似乎是倒置的。

您可以通过以下方式解决此问题:

  • 将原点向上移动视图的高度。
  • 否定(乘以 -1)y 轴。
于 2012-11-21T07:03:28.213 回答
0

两全其美,使用 UIImagedrawAtPoint:drawInRect:同时指定您的自定义上下文:

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

CGContextTranslateCTM您也可以使用or来避免修改上下文CGContextScaleCTM

于 2013-08-07T05:46:23.910 回答