Ios 在UIImageView上绘制,而不替换其原始图像

Ios 在UIImageView上绘制,而不替换其原始图像,ios,objective-c,uiimageview,core-graphics,Ios,Objective C,Uiimageview,Core Graphics,此代码对imageview进行更改,但会替换其原始图像。我需要对原始图像进行编辑,而不是替换它 - (void)drawShapes { //NSLog(@"In drawShapes!"); UIGraphicsBeginImageContext(self.planView.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); for(myShape *i in _collect

此代码对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();

图像不会消失,但也不会绘制。我需要在不删除原始照片的情况下进行绘制。

您不能执行
imagview.image
,因为它将用新图像替换旧图像。如果您计划在图像视图上绘制新内容,请尝试在
imageview
上添加子层


将图像转换为cgiimage并将其添加到图层。您应该将
UIGraphicsGetImageFromCurrentImageContext()
转换为
UIGraphicsGetImageFromCurrentImageContext()。CGImage

您需要创建一个
CALayer
并将CGI图像添加到
层。contents
并将该层用作子层

CALayer *layer = [CALayer layer];
layer.contents = (id)UIGraphicsGetImageFromCurrentImageContext().CGImage;
这样做:

[self.imageView.layer addSublayer:layer];

您不能执行
imagview.image
,因为它将用新图像替换旧图像。如果您计划在图像视图上绘制新内容,请尝试在
imageview
上添加子层


将图像转换为cgiimage并将其添加到图层。您应该将
UIGraphicsGetImageFromCurrentImageContext()
转换为
UIGraphicsGetImageFromCurrentImageContext()。CGImage

您需要创建一个
CALayer
并将CGI图像添加到
层。contents
并将该层用作子层

CALayer *layer = [CALayer layer];
layer.contents = (id)UIGraphicsGetImageFromCurrentImageContext().CGImage;
这样做:

[self.imageView.layer addSublayer:layer];


你怎么称呼drawshapes?我这样称呼它[自我drawshapes];您不能执行imagview.image,因为它将用新图像替换旧图像。如果您计划在图像视图上绘制新内容,请尝试在imageviewUIGraphicsGetImageFromCurrentImageContext()上添加子层。此操作将返回拒绝执行此操作的图像:[self.planView.layer addSublayer:UIGraphicsGetImageFromCurrentImageContext()];您应该将UIGraphicsGetImageFromCurrentImageContext()转换为UIGraphicsGetImageFromCurrentImageContext().CGImage;你怎么称呼drawshapes?我这样称呼它[自我drawshapes];您不能执行imagview.image,因为它将用新图像替换旧图像。如果您计划在图像视图上绘制新内容,请尝试在imageviewUIGraphicsGetImageFromCurrentImageContext()上添加子层。此操作将返回拒绝执行此操作的图像:[self.planView.layer addSublayer:UIGraphicsGetImageFromCurrentImageContext()];您应该将UIGraphicsGetImageFromCurrentImageContext()转换为UIGraphicsGetImageFromCurrentImageContext().CGImage;对于我的任务,除了第一次,我不会制作addsublayer,并继续编辑该层CALayer*layer=[CALayer layer];行给出链接器错误可能您应该将QuartzCore框架添加到您的项目中。架构i386的未定义符号:“\u OBJC\u CLASS\u$\ u CALayer”,引用自ECS189DrawingViewController中的:OBJC CLASS ref。o ld:未找到架构i386的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)我会调查的。我在你为我的任务提供的链接上看不到文件。除了第一次,我不会创建addsublayer,并继续编辑该层CALayer*layer=[CALayer layer];行给出链接器错误可能您应该将QuartzCore框架添加到您的项目中。架构i386的未定义符号:“\u OBJC\u CLASS\u$\ u CALayer”,引用自ECS189DrawingViewController中的:OBJC CLASS ref。o ld:未找到架构i386的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)我会调查的。我在你提供的链接上看不到文件