1

我已经使用使用 OpenGL 的 GLPaint 演示应用程序成功实现了一个绘图应用程序。现在我的问题是我没有办法实现上一个/下一个功能,其中显示上一个/下一个图像的笔画,我可以从我之前离开的地方继续我的绘图。

任何帮助引导我走向正确方向的帮助表示赞赏。

请找到如下所示的代码

-(void)showImagePrevious{
NSMutableString *fileName = [NSString stringWithFormat:@"%d.png",self.imageCounter+1];
fileName  = (NSMutableString *)[fileName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@", fileName]];


// Write image to PNG
[UIImageJPEGRepresentation([self createImageFromGLView:self], 1.0) writeToFile:jpgPath atomically:YES];

// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
NSLog(@"%d",self.imageCounter);
[imgBackground1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",self.imageCounter]]];
[self erase];
NSString  *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",[NSString stringWithFormat:@"%d.png",self.imageCounter]]];

NSLog(@"%@",filePath);
NSData *texData = [[NSData alloc] initWithContentsOfFile:filePath];

imgDrawnImage.image =  [[UIImage alloc] initWithData:texData];


if (imgDrawnImage.image == nil)
    NSLog(@"Do real error checking here");

GLuint width = CGImageGetWidth(imgDrawnImage.image.CGImage);
GLuint height = CGImageGetHeight(imgDrawnImage.image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context1 = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context1, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context1, 0, height - height );
CGContextDrawImage( context1, CGRectMake( 0, 0, width, height ), imgDrawnImage.image.CGImage );


CGContextRelease(context1);

free(imageData);

}

-(void)showImageNext{
NSMutableString *fileName = [NSString stringWithFormat:@"%d.png",self.imageCounter-1];
fileName  = (NSMutableString *)[fileName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@", fileName]];


// Write image to PNG
[UIImageJPEGRepresentation([self createImageFromGLView:self], 1.0) writeToFile:jpgPath atomically:YES];

// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
NSLog(@"%d",self.imageCounter);
[imgBackground1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",self.imageCounter]]];
[self erase];
NSString  *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",[NSString stringWithFormat:@"%d.png",self.imageCounter]]];

NSLog(@"%@",filePath);

NSData *texData = [[NSData alloc] initWithContentsOfFile:filePath];

imgDrawnImage.image =  [[UIImage alloc] initWithData:texData];

if (imgDrawnImage.image == nil)
    NSLog(@"Do real error checking here");

GLuint width = CGImageGetWidth(imgDrawnImage.image.CGImage);
GLuint height = CGImageGetHeight(imgDrawnImage.image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context1 = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context1, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context1, 0, height - height );
CGContextDrawImage( context1, CGRectMake( 0, 0, width, height ), imgDrawnImage.image.CGImage );

//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

CGContextRelease(context1);

free(imageData);
}

此方法用于创建要保存在文档目录中的 UIImage,我将 self(MyCanvasView) 作为参数传递

-(UIImage *)createImageFromGLView:(UIView *)glView
{
int width = glView.frame.size.height;
int height = glView.frame.size.width;

NSInteger myDataLength = width * height * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < height; y++)
{
    for(int x = 0; x < width * 4; x++)
    {
        buffer2[((height - 1) - y) * width * 4 + x] = buffer[y * 4 * width + x];
    }
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}

最好的,

阿迪亚

4

0 回答 0