The following code is intended to inset an image into the PDF at the point the user has tapped. The code works well the first time. However, every time I try and run the code again to add a second image, I get an EXC_BAD_ACCESS from within the Radaee library (when addContent is called). Is there any way you can explain what is going wrong, and potentially how to work around this?
- (void)OnSingleTapped:(float)x :(float)y :(NSString *)text
{
x = x - 200;
y = y + 100;
NSNumber* xx = [NSNumber numberWithInt:x];
NSNumber* yy = [NSNumber numberWithInt:y];
struct PDFV_POS pos;
[m_view vGetPos:&pos];
int pageNo = pos.pageno;
PDFPage* p = [m_doc page:pageNo];
PDFVPage* vp = [m_view vGetPage:pageNo];
UIImage* temp = [UIImage imageNamed:@"Signature"];
NSString* path = NSTemporaryDirectory();
path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", [[NSUUID new] UUIDString]]];
BOOL written = [UIImageJPEGRepresentation(temp, 0) writeToFile:path atomically:YES];
NSLog(@"Temp File Written? %@", [NSNumber numberWithBool:written]);
PDFDocGState* gs = [m_doc newGState];
[gs setFillAlpha:255];
[gs setStrokeAlpha:255];
PDF_PAGE_GSTATE ppgs = [p addResGState:gs];
PDFDocImage* i = [m_doc newImageJPEG:path];
PDF_PAGE_IMAGE ii = [p addResImage:i];
PDFPageContent* newContent = [[PDFPageContent alloc] init];
[newContent gsSet:ppgs];
NSNumber* scrollx = [NSNumber numberWithInt:[m_view vGetX]];
NSNumber* scrolly = [NSNumber numberWithInt:[m_view vGetY]];
float pdfx = [vp ToPDFX:[xx floatValue] : [scrollx floatValue]];
float pdfy = [vp ToPDFX:[yy floatValue] : [scrolly floatValue]];
int pageHeight = [vp GetHeight] / [vp GetScale];
pdfy = pageHeight - pdfy;
PDFMatrix* m = [[PDFMatrix alloc] init:110 :65 :pdfx :pdfy];
[newContent gsCatMatrix:m];
[newContent drawImage:ii];
[newContent gsRestore];
BOOL added = [p addContent:newContent :YES];
NSLog(@"Content Added? %@", [NSNumber numberWithBool:added]);
[m_view vRenderPage:vp];
[m_doc save];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL deleted = [fileManager removeItemAtPath:path error:NULL];