Ios UIImage上的绘图问题
我使用下面的代码来画一个图像。所有触摸代理都在工作,但图形不会在UIImage上渲染。这是代码。我无法确定这个问题。提前谢谢Ios UIImage上的绘图问题,ios,cgcontext,Ios,Cgcontext,我使用下面的代码来画一个图像。所有触摸代理都在工作,但图形不会在UIImage上渲染。这是代码。我无法确定这个问题。提前谢谢 - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { [self setupVariables]; } return self; } - (void)drawPic:(UIImage *)thisPic
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self setupVariables];
}
return self;
}
- (void)drawPic:(UIImage *)thisPic {
myPic = thisPic;
[myPic retain];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
float newHeight;
float newWidth;
float xPos;
float yPos;
float ratio;
if (myPic != NULL) {
ratio = myPic.size.height/460;
if (myPic.size.width/320 > ratio) {
ratio = myPic.size.width/320;
}
newHeight = myPic.size.height/ratio;
newWidth = myPic.size.width/ratio;
xPos = (320 - newWidth) / 2;
yPos = (460 - newHeight) / 2;
[myPic drawInRect:CGRectMake(xPos, yPos, newWidth, newHeight)];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [[event allTouches] allObjects];
if ([allTouches count] > 1) {
return;
}
else {
[self drawPoint:[allTouches objectAtIndex:0]];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [[event allTouches] allObjects];
if ([allTouches count] > 1) {
return;
}
else {
[self drawPoint:[allTouches objectAtIndex:0]];
self.previousPoint = nil;
self.point = nil;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [[event allTouches] allObjects];
if ([allTouches count] > 1) {
return;
}
else {
[self drawPoint:[allTouches objectAtIndex:0]];
self.previousPoint = nil;
self.point = nil;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [[event allTouches] allObjects];
if ([allTouches count] > 1) {
return;
}
else {
[self drawPoint:[allTouches objectAtIndex:0]];
}
}
- (void)dealloc {
CGContextRelease(offScreenBuffer);
[point release];
[previousPoint release];
[super dealloc];
}
- (void)setupVariables {
self.point = nil;
self.previousPoint = nil;
offScreenBuffer = [self setupBuffer];
}
- (CGContextRef)setupBuffer {
CGSize size = self.bounds.size;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,size.width,size.height,8,size.width*4, colorSpace,kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
return context;
}
- (void)drawToBuffer {
// Red Gr Blu Alpha
CGFloat color[4] = {0.0, 1.0, 0.0, 1.0};
if (self.previousPoint != nil) {
CGContextSetRGBStrokeColor(offScreenBuffer, color[0], color[1], color[2], color[3]);
CGContextBeginPath(offScreenBuffer);
CGContextSetLineWidth(offScreenBuffer, 10.0);
CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);
CGContextMoveToPoint(offScreenBuffer, previousPoint.location.x, previousPoint.location.y);
CGContextAddLineToPoint(offScreenBuffer, point.location.x, point.location.y);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeColor);
CGContextDrawPath(offScreenBuffer, kCGPathStroke);
}
}
- (void)drawPoint:(UITouch *)touch {
PointLocation *currentLoc = [[PointLocation alloc] init];
currentLoc.location = [touch locationInView:self];
self.previousPoint = self.point;
self.point = currentLoc;
[self drawToBuffer];
[self setNeedsDisplay];
[currentLoc release];
}
这很简单。您需要在
drawRect:
中进行所有绘制,并且您只在drawRect:
中绘制图像,因此您只能看到图像
-(void)drawPic:(UIImage *)thisPic {
//code for UIViewContentModeScaleAspectFit if needed
self.backgroundColor = [UIColor colorWithPatternImage:thisPic];
[self setNeedsDisplay];
}
请勿在drawRect方法中使用此选项:
[myPic drawInRect:CGRectMake(xPos、YPO、newWidth、newHeight)]
编辑
- (void)drawRect:(CGRect)rect {
if (!myDrawing) {
// CGFloat scale = 0.5;
// CATransform3D transform = CATransform3DMakeScale(scale, scale, scale);
// [self layer].transform = transform;
myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
self.lineOnOffArray = [[NSMutableArray alloc] initWithCapacity:0];
//arrPenThickness = [[NSMutableArray alloc] initWithCapacity:0];
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
if ([myDrawing count] > 0) {
for (int i = 0 ; i < [myDrawing count] ; i++) {
NSArray *thisArray = [myDrawing objectAtIndex:i];
NSString *onOffFlag = [self.lineOnOffArray objectAtIndex:i];
if (([thisArray count] > 2) && (onOffFlag == @"1")){
float penT = [[arrPenThickness objectAtIndex:i] floatValue];
NSString* penC = [arrPenColors objectAtIndex:i];
NSArray *arr = [penC componentsSeparatedByString:@","];
float r = [[arr objectAtIndex:0]floatValue];
float g = [[arr objectAtIndex:1]floatValue];
float b = [[arr objectAtIndex:2]floatValue];
float a = [[arr objectAtIndex:3]floatValue];
CGContextSetRGBStrokeColor(ctx,
r/255.0,
g/255.0,
b/255.0,
a);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, penT);
float thisX = [[thisArray objectAtIndex:0] floatValue];
float thisY = [[thisArray objectAtIndex:1] floatValue];
CGContextBeginPath(ctx);
for (int j = 2; j < [thisArray count] ; j+=2) {
CGContextMoveToPoint(ctx, thisX, thisY);
thisX = [[thisArray objectAtIndex:j] floatValue];
thisY = [[thisArray objectAtIndex:j+1] floatValue];
CGContextAddLineToPoint(ctx, thisX,thisY);
}
CGContextStrokePath(ctx);
}
}
}
}
-(void)drawRect:(CGRect)rect{
如果(!myDrawing){
//CGFloat刻度=0.5;
//CATTransformM3D transform=CATTransformM3DMakeScale(缩放,缩放,缩放);
//[self layer].transform=transform;
myDrawing=[[NSMutableArray alloc]initWithCapacity:0];
self.lineOnOffArray=[[NSMutableArray alloc]initWithCapacity:0];
//arrPenThickness=[[NSMutableArray alloc]initWithCapacity:0];
}
CGContextRef ctx=UIGraphicsGetCurrentContext();
如果([myDrawing count]>0){
对于(int i=0;i<[myDrawing count];i++){
NSArray*thisArray=[myDrawing objectAtIndex:i];
NSString*onOffFlag=[self.lineOnOffArray对象索引:i];
如果(([thisArray count]>2)和&(onOffFlag==@“1”)){
float penT=[[arrPenThickness objectAtIndex:i]floatValue];
NSString*penC=[arrPenColors objectAtIndex:i];
NSArray*arr=[PEC组件由字符串分隔:@“,”];
浮点r=[[arr objectAtIndex:0]floatValue];
float g=[[arr objectAtIndex:1]floatValue];
浮动b=[[arr objectAtIndex:2]floatValue];
浮点a=[[arr objectAtIndex:3]floatValue];
CGContextSetRGBStrokeColor(ctx,
r/255.0,
g/255.0,
b/255.0,
a) );
CGContextSetLineCap(ctx、kCGLineCapRound);
CGContextSetLineWidth(ctx,penT);
float thisX=[[thisArray对象索引:0]floatValue];
float thisY=[[thisArray对象索引:1]floatValue];
CGContextBeginPath(ctx);
对于(int j=2;j<[此数组计数];j+=2){
CGContextMoveToPoint(ctx、thisX、thisY);
thisX=[[thisArray objectAtIndex:j]floatValue];
thisY=[[thisArray对象索引:j+1]floatValue];
CGContextAddLineToPoint(ctx、thisX、thisY);
}
CGContextStrokePath(ctx);
}
}
}
}
亲爱的沃里夫,我照你说的做了。但我仍然得到了与之前相同的错误:“CGContextSetBlendMode:无效上下文0x0”。如果我没有在drawRect方法中添加图像,而是使用以下代码CGImageRef cgImage=CGBitmapContextCreateImage(offScreenBuffer);UIImage*UIImage=[[UIImage alloc]initWithCGImage:cgImage];CGImageRelease(cgImage);[uiImage drawInRect:self.bounds];[图片发布];我将能画出一幅全景图。但我正试图画出图像并将其保存在设备的照片库中。请帮帮我,回答我。可变数组与您的代码不匹配。请参见如何在循环中使用CGContextAddLineToPoint。我不需要CGContextSetBlendMode。