Ios 获取仅绘制特定区域边界的UIImage-PaintView
我已经使用以下工具实现了绘制/绘制:Ios 获取仅绘制特定区域边界的UIImage-PaintView,ios,objective-c,iphone,uiimage,paint,Ios,Objective C,Iphone,Uiimage,Paint,我已经使用以下工具实现了绘制/绘制: - (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event -(void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 现在的问题是,对于任何绘制的线,我都希望得
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
-(void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
现在的问题是,对于任何绘制的线,我都希望得到特定的线/绘制图像。我不想要整个屏幕的图像,只需要绘制区域/线条边界/绘画
原因是我想在绘制的线条/绘画上执行平移手势/删除功能
用户可以绘制多条线,因此需要分别为所有这些线绘制UIImage
任何逻辑或代码片段都会非常有用
提前感谢根据您的应用程序,特别是您计划在一行中执行此操作的次数,您可以为每条绘制线创建不同的图像/层。最终的图像将基本上是所有单独的线条相互叠加 创建自定义视图以捕获触摸事件可能更有效。您可以存储每条绘制线的触摸坐标列表,并在自定义drawRect中一次性渲染它们。这样,您可以存储每条绘制线的坐标列表,并且仍然可以访问每条绘制线,而不是图像列表。可以根据用于渲染线的坐标计算面积/边界
附加的上下文和代码可能会有所帮助,我不确定我是否完全理解您试图实现的目标 我来看看MVPaint项目。似乎你有一个目标:
MVPaintDrawing
\u图纸
它包含一个MVPaintTransaction
数组。您可以在那些MVPaintTransaction
上迭代以绘制UIImage
因此,首先可以添加一个方法,从MVPaintTransaction
获取图像:
- (UIImage *) imageToDrawWithSize:(CGSize) size xScale:(CGFloat)xScale yScale:(CGFloat)yScale {
UIGraphicsBeginImageContext(size);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), xScale, yScale);
// call the existing draw method
[self draw];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
然后添加一个方法,从MVPaintDrawing
类中的MVPaintTransaction
数组中获取图像数组:
- (NSArray *) getImagesFromDrawingOnSurface: (UIImageView *) surface xScale: (CGFloat) xScale yScale: (CGFloat) yScale{
NSMutableArray *imageArray = [NSMutableArray new];
for (MVPaintTransaction * transaction in _drawing) {
UIImage *image = [transaction imageToDrawWithSize:surface.frame.size xScale:xScale yScale:yScale];
[imageArray addObject:image];
}
return imageArray;
}
- (UIImage *)trimmedImage:(UIImage *)img {
CGImageRef inImage = img.CGImage;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
size_t width = CGImageGetWidth(inImage);
size_t height = CGImageGetHeight(inImage);
CGPoint top,left,right,bottom;
BOOL breakOut = NO;
for (int x = 0;breakOut==NO && x < width; x++) {
for (int y = 0; y < height; y++) {
int loc = x + (y * width);
loc *= 4;
if (m_PixelBuf[loc + 3] != 0) {
left = CGPointMake(x, y);
breakOut = YES;
break;
}
}
}
breakOut = NO;
for (int y = 0;breakOut==NO && y < height; y++) {
for (int x = 0; x < width; x++) {
int loc = x + (y * width);
loc *= 4;
if (m_PixelBuf[loc + 3] != 0) {
top = CGPointMake(x, y);
breakOut = YES;
break;
}
}
}
breakOut = NO;
for (int y = height-1;breakOut==NO && y >= 0; y--) {
for (int x = width-1; x >= 0; x--) {
int loc = x + (y * width);
loc *= 4;
if (m_PixelBuf[loc + 3] != 0) {
bottom = CGPointMake(x, y);
breakOut = YES;
break;
}
}
}
breakOut = NO;
for (int x = width-1;breakOut==NO && x >= 0; x--) {
for (int y = height-1; y >= 0; y--) {
int loc = x + (y * width);
loc *= 4;
if (m_PixelBuf[loc + 3] != 0) {
right = CGPointMake(x, y);
breakOut = YES;
break;
}
}
}
CGFloat scale = img.scale;
CGRect cropRect = CGRectMake(left.x / scale, top.y/scale, (right.x - left.x)/scale, (bottom.y - top.y) / scale);
UIGraphicsBeginImageContextWithOptions( cropRect.size,
NO,
scale);
[img drawAtPoint:CGPointMake(-cropRect.origin.x, -cropRect.origin.y)
blendMode:kCGBlendModeCopy
alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CFRelease(m_DataRef);
return croppedImage;
}
通过这种方式,您将拥有一个与您所绘制的每条线对应的UIImage
数组。如果希望这些图像具有“最小”的可能大小(我的意思是没有额外的alpha部分),可以应用(我在MVPaintTransaction
类中添加了它):
借
如果你发布了你的绘画/绘画方法,这样我自己或其他人就可以更容易地完成这一点,这将是非常有帮助的。我在发布之前也尝试过,如果我从DrawingOnSurface调用getImagesFromDrawingOnSurface,我会得到一个UIImage数组(每个图像代表一条你要求的画线)。我的代码不会改变行的显示方式(例如,要做到这一点,您可能需要创建UIImageView数组)。
return result;
return [self trimmedImage:result];