【问题标题】:How to save an image in photolibrary after drawing a rectangle on imageview with the finger in iPad?如何在 iPad 中用手指在图像视图上绘制矩形后将图像保存在照片库中?
【发布时间】:2012-07-03 11:36:58
【问题描述】:

在我的应用程序中,我用手指在图像视图上绘制了一个矩形。矩形是使用drawRect 方法创建的,并添加到该图像视图中。

现在我的问题是我必须将该图像与这些矩形一起存储在照片库中。就像在 imageview 上有矩形的图像。

谁能帮助我找到如何做到这一点?

这是我的drawRect 方法代码:

-(void)drawRect:(CGRect)rect 
{
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(contextRef, 5.0);
    CGContextSetRGBStrokeColor(contextRef, 255.0, 1.0, 0.0, 1.0);
    CGContextSetStrokeColorWithColor(contextRef,[UIColor redColor].CGColor);

    // Draw the border along the view edge.
    CGContextStrokeRect(contextRef, rect); 
}

【问题讨论】:

  • 如何绘制矩形?你能在这里显示代码吗?
  • -(void)drawRect:(CGRect)rect { CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(contextRef, 5.0); CGContextSetRGBStrokeColor(contextRef, 255.0, 1.0, 0.0, 1.0); CGContextSetStrokeColorWithColor(contextRef,[UIColor redColor].CGColor); // 沿视图边缘绘制边框 CGContextStrokeRect(contextRef, rect); } }
  • 以这种方式创建矩形并使用 addsubview 在 imageview 上添加此视图
  • @Naresh - 问题上有一个“编辑”按钮,可以让问题所有者编辑和更新问题。这有助于澄清,尤其是发布代码 - 因为这些小 cmets 中的代码很难阅读。干杯!

标签: ios ipad cgcontext photolibrary


【解决方案1】:

方法参数view 是添加图像和绘图的视图。调用此方法将返回一个 UIImage。

-(UIImage*)convertViewToImage:(UIView*)view
{ 
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], CGRectMake(0, 0, view.frame.size.width, view.frame.size.height));
UIImage *targetImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return targetImage;
} 

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2020-11-28
    • 1970-01-01
    相关资源
    最近更新 更多