Ios 如何从UIVIEW中仅获取绘图零件(用作绘图板)?
以上链接我在这个项目中作为drawingpad应用程序的参考。 现在,我只想从绘图板的绘图部分。 就像这里是在绘图板上,我在里面画了一个。现在我只想画一个我在绘图板上画过的区域。不是全部Ios 如何从UIVIEW中仅获取绘图零件(用作绘图板)?,ios,uiimage,crop,getpixel,Ios,Uiimage,Crop,Getpixel,以上链接我在这个项目中作为drawingpad应用程序的参考。 现在,我只想从绘图板的绘图部分。 就像这里是在绘图板上,我在里面画了一个。现在我只想画一个我在绘图板上画过的区域。不是全部 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (self.textView && !self.textView.hidden) { [self comm
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.textView && !self.textView.hidden) {
[self commitAndHideTextEntry];
return;
}
// add the first touch
UITouch *touch = [touches anyObject];
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
// init the bezier path
self.currentTool = [self toolWithCurrentSettings];
self.currentTool.lineWidth = self.lineWidth;
self.currentTool.lineColor = self.lineColor;
self.currentTool.lineAlpha = self.lineAlpha;
if ([self.currentTool isKindOfClass:[ACEDrawingTextTool class]]) {
[self initializeTextBox: currentPoint];
}
else {
[self.pathArray addObject:self.currentTool];
[self.currentTool setInitialPoint:currentPoint];
}
// call the delegate
if ([self.delegate respondsToSelector:@selector(drawingView:willBeginDrawUsingTool:)]) {
[self.delegate drawingView:self willBeginDrawUsingTool:self.currentTool];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// save all the touches in the path
UITouch *touch = [touches anyObject];
previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
if ([self.currentTool isKindOfClass:[ACEDrawingPenTool class]]) {
CGRect bounds = [(ACEDrawingPenTool*)self.currentTool addPathPreviousPreviousPoint:previousPoint2 withPreviousPoint:previousPoint1 withCurrentPoint:currentPoint];
CGRect drawBox = bounds;
drawBox.origin.x -= self.lineWidth * 2.0;
drawBox.origin.y -= self.lineWidth * 2.0;
drawBox.size.width += self.lineWidth * 4.0;
drawBox.size.height += self.lineWidth * 4.0;
[self setNeedsDisplayInRect:drawBox];
}
else if ([self.currentTool isKindOfClass:[ACEDrawingTextTool class]]) {
[self resizeTextViewFrame: currentPoint];
}
else {
[self.currentTool moveFromPoint:previousPoint1 toPoint:currentPoint];
[self setNeedsDisplay];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// make sure a point is recorded
[self touchesMoved:touches withEvent:event];
if ([self.currentTool isKindOfClass:[ACEDrawingTextTool class]]) {
[self startTextEntry];
}
else {
[self finishDrawing];
}
}
- (void)finishDrawing
{
// update the image
[self updateCacheImage:NO];
// clear the redo queue
[self.bufferArray removeAllObjects];
// call the delegate
if ([self.delegate respondsToSelector:@selector(drawingView:didEndDrawUsingTool:)]) {
[self.delegate drawingView:self didEndDrawUsingTool:self.currentTool];
}
// clear the current tool
self.currentTool = nil;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// make sure a point is recorded
[self touchesEnded:touches withEvent:event];
}
#pragma mark - Text Entry
- (void) initializeTextBox: (CGPoint)startingPoint {
if (!self.textView) {
self.textView = [[UITextView alloc] init];
self.textView.delegate = self;
self.textView.returnKeyType = UIReturnKeyDone;
self.textView.autocorrectionType = UITextAutocorrectionTypeNo;
self.textView.backgroundColor = [UIColor clearColor];
self.textView.layer.borderWidth = 1.0f;
self.textView.layer.borderColor = [[UIColor grayColor] CGColor];
self.textView.layer.cornerRadius = 8;
[self.textView setContentInset: UIEdgeInsetsZero];
[self addSubview:self.textView];
}
int calculatedFontSize = self.lineWidth * 3; //3 is an approximate size factor
[self.textView setFont:[UIFont systemFontOfSize:calculatedFontSize]];
self.textView.textColor = self.lineColor;
self.textView.alpha = self.lineAlpha;
int defaultWidth = 200;
int defaultHeight = calculatedFontSize * 2;
int initialYPosition = startingPoint.y - (defaultHeight/2);
CGRect frame = CGRectMake(startingPoint.x, initialYPosition, defaultWidth, defaultHeight);
frame = [self adjustFrameToFitWithinDrawingBounds:frame];
self.textView.frame = frame;
self.textView.text = @"";
self.textView.hidden = NO;
}
- (IBAction)takeScreenshot:(id)sender
{
[redoimage removeAllObjects];
// _previewImageView.frame=CGRectMake(x, y, w,h);
self.previewImageView.image = self.drawingView.image;////herer i can get image from the drawpad.
// self.previewImageView.hidden = NO;
CGSize nsize={100,100};
CGImageRef imageref = [self.drawingView.image CGImage];
UIImage *newimg=[self ManipulateImagePixelData:imageref];
UIImage *newimage=[self scaleImage:newimg toSize:nsize];
// self.previewImageView.image=newimage;
}
////////// this code use geting pixel data from the Uimage/////
-(UIImage *) ManipulateImagePixelData:(CGImageRef) inImage
{
// Create the bitmap context
CGContextRef cgctx = [self CreateARGBBitmapContext:inImage];
if (cgctx == NULL)
{
// error creating context
return NULL;
}
// Get image width, height. We'll use the entire image.
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage);
CGImageRef imageRef = CGBitmapContextCreateImage(cgctx);
UIImage * finalImage = [UIImage imageWithCGImage:imageRef];
// Now we can get a pointer to the image data associated with the bitmap
// // context.
// void *data = CGBitmapContextGetData (cgctx);
// if (data != NULL)
// {
//
// // **** You have a pointer to the image data ****
//
// // **** Do stuff with the data here ****
//
// }
// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context
// if (data)
// {
// free(data);
// }
return finalImage;
}
- (CGContextRef) CreateARGBBitmapContext:(CGImageRef) inImage
{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
// Get image width, height. We'll use the entire image.
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
}
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free (bitmapData);
fprintf (stderr, "Context not created!");
}
// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );
return context;
}
提前感谢。[自行完成图纸];加上这个function@DivinDesert我添加了[self finishDrawing]函数。Oops在您实际绘制的位置添加函数,以便我可以提供帮助you@DivineDesert我画在我的视野里。然后我从UIView中获取图像。在这里,我使用2个文件1.ACEDrawingView和2.acedrawingTools,从这个链接的应用程序中查看: