所以我一直在使用 sqlite3 和其中包含 pdf 的 blob。我从 db 中读取它,在加载 2 个请求后,我在模拟器中收到错误。
视图控制器.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
视图控制器.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] 上引发的;因为如果我在它之后使用停止加载,则不会发生错误,或者如果我评论该行,但我真的不知道这里有什么问题......