2012-02-29 30 views
5
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
UIGraphicsBeginImageContext(drawImage.frame.size); 
[drawImage.image drawInRect:CGRectMake(0,0, drawImage.frame.size.width, drawImage.frame.size.height)]; 

CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0); 
UIBezierPath *path=[self pathFromPoint:currentPoint 
           toPoint:currentPoint]; 

CGContextBeginPath(currentContext); 
CGContextAddPath(currentContext, path.CGPath); 
CGContextDrawPath(currentContext, kCGPathFill); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

Trong mã ở trên CGContextRef currentContext được tạo từ UIGraphicsGetCurrentContext() và chuyển đến CGContextBeginPath CGContextAddPath CGContextDrawPath currentContext có thông số với chúng không hoạt động đối với tôi! Khi tôi đang làm touchMovie.UIGraphicsGetCurrentGiá trị văn bản truyền qua CGContextRef không hoạt động?

Khi tôi chuyển trực tiếp UIGraphicsGetCurrentContext() thay cho số currentContext làm việc cho tôi. Tôi muốn biết tại sao lại như thế?

@Tất cả hãy tư vấn cho tôi về vấn đề này.

Trả lời

7

Vấn đề là currentContext không còn là bối cảnh hiện nay sau khi bạn bắt đầu một bối cảnh hình ảnh:

CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
UIGraphicsBeginImageContext(drawImage.frame.size); 
// Now the image context is the new current context. 

Vì vậy, bạn nên đảo ngược hai dòng sau:

UIGraphicsBeginImageContext(drawImage.frame.size); 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

Sửa

Như Nicolas đã lưu ý, bạn nên kết thúc bối cảnh hình ảnh khi bạn không còn cần đến nó nữa:

drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); // Add this line. 

Sửa

Cũng cần chú ý rằng bạn đang thiết lập một đột quỵ màu nhưng vẽ với điền lệnh.

Vì vậy, bạn nên gọi phương thức màu thích hợp thay vì:

CGContextSetRGBFillColor(currentContext, 0.0, 1.0, 0.0, 1.0); 
+0

Hơn nữa, bạn nên kết thúc ngữ cảnh hình ảnh khi không cần đến nữa để khôi phục ngăn xếp đồ họa ở trạng thái trước đó: 'UIGraphicsEndImageContext();' –

+0

@Sch làm việc cho tôi Cảm ơn Sch – kiran

+0

tôi tìm thấy một vấn đề khác về điều này! tôi không thể thiết lập màu cho CGContextSetRGBStrokeColor này (currentContext, 1.0, 0.0, 0.0, 1.0); nếu tôi đặt nó thành màu đỏ thì nó vẫn vẽ màu đen! – kiran

1

UIGraphicsBeginImageContext tạo ra một bối cảnh mới và đặt nó ở bối cảnh hiện nay. Vì vậy, bạn nên làm

CGContextRef currentContext = UIGraphicsGetCurrentContext();

sauUIGraphicsBeginImageContext. Nếu không, bạn tìm nạp ngữ cảnh và bối cảnh này ngay lập tức bị thay thế bởi UIGraphicsBeginImageContext.

Các vấn đề liên quan