drawRect在iPhone中反转imageView(模拟器)
我看到了,但这并不能消除我的疑虑 我有一个自定义的imageView类,基本上是一个子类。我需要在UIView上以特定像素值添加子图像视图。当我使用自定义imageview(draw rect)绘制这些图像时,我的图像是反转的 请帮助我,我需要在我的框架中做什么样的改变,以使图像不倒置 下面是图片 CustomImageView.mdrawRect在iPhone中反转imageView(模拟器),iphone,ios,uiimageview,drawrect,Iphone,Ios,Uiimageview,Drawrect,我看到了,但这并不能消除我的疑虑 我有一个自定义的imageView类,基本上是一个子类。我需要在UIView上以特定像素值添加子图像视图。当我使用自定义imageview(draw rect)绘制这些图像时,我的图像是反转的 请帮助我,我需要在我的框架中做什么样的改变,以使图像不倒置 下面是图片 CustomImageView.m @implementation CustomImageView @synthesize customImage; - (id)initWithFrame:(CGR
@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];
}
是的,这是正常的行为 Uikit左上角坐标(0,0)
OpenGL/CoreGraphics左下角坐标(0,0) 因此,你的形象似乎是颠倒的 您可以通过以下方式解决此问题:
- 将原点向上移动视图的高度。
- 否定(乘以-1) y轴
drawAtPoint:
或drawInRect:
:
UIGraphicsPushContext(context);
[image drawAtPoint:CGPointZero];
UIGraphicsPopContext();
此外,您还可以使用
cgcontexttranslatecm
或CGContextScaleCTM
无效修改您的上下文。这是两种不同的方法还是一种方法有两个步骤?我使用的是UIKit,我从左上角获取像素值。。所以我不需要做任何更改,对吗?不,实际上,当你使用CGContextRef在drawrect中绘图时,你必须这样做,因为你使用的是CoreGraphics-CG-指CoreGraphics扫描你能帮我编写代码sinppnet吗?我无法做到这一点