Signin/Signup with: 
Welcome, Guest
Username: Password: Remember me
Questions about iOS development and PDF
  • Page:
  • 1
  • 2

TOPIC:

EXC_BAD_ACCESS in [PDFPage addContent] 9 years 8 months ago #6982

  • sparklellama
  • sparklellama's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 34
  • Thank you received: 0
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];
	

Please Log in or Create an account to join the conversation.

EXC_BAD_ACCESS in [PDFPage addContent] 9 years 8 months ago #6984

  • radaee
  • radaee's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 1123
  • Thank you received: 73
- (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];//this position from (0,0) of screen.
int pageNo = pos.pageno;
PDFVPage* vp = [m_view vGetPage:pageNo];
if( !vp ) return;
PDFPage* p = [vp Page];
if( !p ) return;
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]);
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 * m_scale floatValue] : [scrollx floatValue]];
float pdfy = [vp ToPDFX:[yy floatValue] : [scrolly floatValue]];
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];

Please Log in or Create an account to join the conversation.

EXC_BAD_ACCESS in [PDFPage addContent] 9 years 8 months ago #6986

  • sparklellama
  • sparklellama's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 34
  • Thank you received: 0
Thank you very much for your help. The code modified as you suggested is simpler and makes more sense, and works well the first time.

However, the second time I tap to add the image to the PDF the second time, the application still crashes with EXC_BAD_ACCESS at the line BOOL added = [p addContent:newContent :YES];

Please Log in or Create an account to join the conversation.

EXC_BAD_ACCESS in [PDFPage addContent] 9 years 8 months ago #6987

  • radaee
  • radaee's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 1123
  • Thank you received: 73
are p is null?

Please Log in or Create an account to join the conversation.

EXC_BAD_ACCESS in [PDFPage addContent] 9 years 8 months ago #6988

  • sparklellama
  • sparklellama's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 34
  • Thank you received: 0
All variables seem to be set up correctly. Here is a screen grab of the exception.

Please Log in or Create an account to join the conversation.

EXC_BAD_ACCESS in [PDFPage addContent] 9 years 8 months ago #6989

  • radaee
  • radaee's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 1123
  • Thank you received: 73
[m_view vGetPos:&pos:x:y];

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Powered by Kunena Forum