2013-10-16 45 views
0

所以我一直在使用sqlite3和其中有pdf的blob。我從數據庫讀取它,並加載2請求後,我在模擬器中出現錯誤。UIWebView loadrequest投擲EXC_BAD_ACCESS代碼= 1

ViewController.m



    #import 

    @interface ViewController : UIViewController { 
     NSMutableArray *lights; 
    } 

    @property (nonatomic, retain) NSMutableArray *lights; 
    @property (weak, nonatomic) IBOutlet UILabel *lightname; 
    @property (weak, nonatomic) IBOutlet UILabel *lighttype; 
    @property (weak, nonatomic) IBOutlet UIImageView *lightmainimage; 
    @property (weak, nonatomic) IBOutlet UIWebView *lightwebview; 

    @property (weak, nonatomic) NSURLRequest *req; 
    @property (weak, nonatomic) NSURL *targetURL; 
    @property (strong, nonatomic) NSString *appFile; 
    @property (weak, nonatomic) NSArray *paths; 
    @property (weak, nonatomic) NSString *documentsDirectory; 

    - (IBAction)GetLightListing:(id)sender; 

    @end 

ViewController.m



    #import "ViewController.h" 
    #import "LightsList.h" 
    #import "MyLightsList.h" 

    @interface ViewController() 
    @end 

    @implementation ViewController 

    @synthesize lightname; 
    @synthesize lighttype; 
    @synthesize lightmainimage; 
    @synthesize lights; 
    @synthesize req; 
    @synthesize targetURL; 
    @synthesize appFile; 
    @synthesize paths; 
    @synthesize documentsDirectory; 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
     MyLightsList *mylights = [[MyLightsList alloc] init]; 
     self.lights = [mylights getMyLights]; 
     [self.lightmainimage setImage:((LightsList *) [self.lights objectAtIndex:0]).lightMainImage]; 
     [self.lighttype setText:((LightsList *) [self.lights objectAtIndex:0]).lightType]; 
     [self.lightname setText:((LightsList *) [self.lights objectAtIndex:0]).lightName]; 
     self.paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     self.documentsDirectory = [self.paths objectAtIndex:0]; 
     NSError *error; 
     self.documentsDirectory = [self.paths objectAtIndex:0]; 
     self.appFile = [self.documentsDirectory stringByAppendingPathComponent:@"datasheet.pdf"]; 
     if([((LightsList *)[self.lights objectAtIndex:0]).lightDatasheetPDF writeToFile:self.appFile options:NSDataWritingAtomic error:&error]) { 
      NSLog(@"file suceed!"); 
     } else NSLog(@"file fail! %@",error); 
     self.targetURL = [NSURL fileURLWithPath:self.appFile]; 
     self.req = [NSURLRequest requestWithURL:self.targetURL]; 
     [self.lightwebview loadRequest:self.req]; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

    - (IBAction)GetLightListing:(id)sender { 
     static NSInteger currentIndex = 0; 
     if(++currentIndex == [self.lights count]) { 
      currentIndex = 0; 
     } else { 
      LightsList *aLight = (LightsList *) [self.lights objectAtIndex:currentIndex]; 
      [self.lightmainimage setImage:aLight.lightMainImage]; 
      [self.lighttype setText:aLight.lightType]; 
      [self.lightname setText:aLight.lightName]; 
      NSError *error; 
      if([aLight.lightDatasheetPDF writeToFile:self.appFile options:NSDataWritingAtomic error:&error]) { 
       NSLog(@"file suceed!"); 
      } else NSLog(@"file fail! %@",error); 
      self.targetURL = [NSURL fileURLWithPath:self.appFile]; 
      self.req = [NSURLRequest requestWithURL:self.targetURL]; 
      [self.lightwebview loadRequest:self.req]; 
     } 
    } 
    @end 

我敢肯定,錯誤會被拋出[self.lightwebview的loadRequest:self.req]。因爲如果我使用後停止加載,錯誤不會發生,或者如果我評論該行,但我真的不知道這裏的問題是什麼...

+1

您應該實現UIWebViewDelegate以查看在崩潰之前調用什麼,什麼不是 – KIDdAe

+0

實現委託方法後,它們都會始終加載(webViewDidStartLoad和webViewDidFinishLoad),但didFailWithError不會。 –

回答

0

問題是在pdf文件中存儲在BLOB,當我打開它們預覽並再次導出它們並將它們再次保存到開始工作的數據庫中。他們顯然有一些無效的數據導致了這一點。

相關問題