此代码对imageview进行了更改,但它替换了原始图像。我需要在原始图像上进行编辑而不是替换它。
- (void)drawShapes {
//NSLog(@"In drawShapes!");
UIGraphicsBeginImageContext(self.planView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
for(myShape *i in _collection) {
[self drawShapesSubroutine:i contextRef:context];
if(i.selected == true) {
CGContextSetLineWidth(context, 1.0f);
CGContextSetStrokeColorWithColor(context, [[UIColor darkGrayColor] CGColor]);
float num[] = {6.0, 6.0};
CGContextSetLineDash(context, 0.0, num, 2);
CGRect rectangle;
[self drawShapeSelector:i selectorRect: &rectangle];
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
//tapped = true;
}
}
if(!skipDrawingCurrentShape && (selectedIndex == -1)) {
[self drawShapesSubroutine:_currentShape contextRef:context];
}
self.planView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
当我删除此行时:
self.planView.image = UIGraphicsGetImageFromCurrentImageContext();
图像不会消失,但也不会消失。我需要在不删除原始照片的情况下进行绘制。
答案 0 :(得分:6)
你不能imagview.image
,因为它会用新的图像替换旧图像。如果您打算在图像视图上绘制新内容,请尝试在imageview
上添加子图层。
将您的图片转换为cgiimage并将其添加到图层。您应该将UIGraphicsGetImageFromCurrentImageContext()
转换为UIGraphicsGetImageFromCurrentImageContext().CGImage
您需要创建一个CALayer
并将您的CGI图像添加到layer.contents
并将该图层用作子图层
CALayer *layer = [CALayer layer];
layer.contents = (id)UIGraphicsGetImageFromCurrentImageContext().CGImage;
并执行此操作:
[self.imageView.layer addSublayer:layer];