我正在使用OpenGL将图像绘制到屏幕上。由于缩放能力,我的图像有时会通过z轴转换。例如,用户的图像大小为3200x2000,他将其放大,因为它不适合屏幕。在缩小之后,他想要裁剪部分图像。我注册选择矩形。然后使用左下角坐标和右上角与左下角之间的距离。坐标的使用方式如下:
//I get one coordinate from mouse down, and then last point from moueDragged events.
float firstx = mouseDownFirst.x * (zoomSlider.floatValue * (-10));
float firsty = mouseDownFirst.y * (zoomSlider.floatValue * (-10));
float lastx = mouseDragLast.x * (zoomSlider.floatValue * (-10));
float lasty = mouseDragLast.y * (zoomSlider.floatValue * (-10));
//Then I draw selection rectangle to screen.
glBegin(GL_QUADS);
glVertex2f(firstx, firsty);
glVertex2f(firstx, lasty);
glVertex2f(lastx, lasty);
glVertex2f(lastx, firsty);
glEnd();
glFlush();
//and do crop. Because image is drawn to framebuffer, I will read frame buffer info and make image from it. Coordinates are being translated back to normal
firstx = firstx / (zoomSlider.floatValue * (-10));
firsty = firsty / (zoomSlider.floatValue * (-10))
lastx = lastx / (zoomSlider.floatValue * (-10));
lasty = lasty / (zoomSlider.floatValue * (-10));
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, [tempRep bitmapData]);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
CIImage *firstImage = [[CIImage alloc] initWithBitmapImageRep:tempRep];
//cropping image to rect and getting result image
float distanceWidth = lastx - firstx;
float distanceHeight = lasty - firsty;
CIImage* secondImage = [firstImage imageByCroppingToRect:CGRectMake(firstx, firsty, distanceWidth, distanceHeight)];
CIFilter* transform = [CIFilter filterWithName:@"CIAffineTransform"];
NSAffineTransform* affineTransform = [NSAffineTransform transform];
[affineTransform translateXBy:-firstx yBy:-firsty];
[transform setValue:affineTransform forKey:@"inputTransform"];
[transform setValue:secondImage forKey:@"inputImage"];
CIImage* lastImage = [transform valueForKey:@"outputImage"];
但它会返回糟糕的图像。不是我选择的。例如:
选择
裁剪图像