开发者

UIImage from a Off Screen view

dear developer i want make an image from an offscreen view.

when i start my app the first view is the RootViewController and everthing works fine.

when i go to an other view and make this.

RootViewController *root = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

UI开发者_运维知识库Image *screenShot = [self imageWithView: root.tableView];

(UIImage *) imageWithView:(UIView *)view

{

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

The app crash in the "imageWithView:(UIView *)view" function at the line [view.layer renderInContext:UIGraphicsGetCurrentContext()];

the log says Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'ListItem''

and this happen in the rootviewcontroller on the line

NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListItem" inManagedObjectContext:self.managedObjectContext];

somebody an idea?


As Rog said there is probably no context. Here is the code that create the one correctly:

-(NSManagedObjectContext*) managedContext {
    @synchronized( self ) {
        if( !_managedObjectContext) {
            _managedObjectContext = [[NSManagedObjectContext alloc] init];
            [_managedObjectContext setPersistentStoreCoordinator: [VIDataManager sharedManager].persistentStoreCoordinator];
        }
    }

    return _managedObjectContext;
}

-(NSPersistentStoreCoordinator*) persistentStoreCoordinator {
    @synchronized( self ) {
        if( !_persistentStoreCoordinator ) {
            NSLog(@"init _persistentStoreCoordinator");
            NSError *error = nil;
            _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: self.managedObjectModel];

            if( ![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL: databaseURL() options:nil error: &error] ) {
            }
        }
    }

    return _persistentStoreCoordinator;    
}

-(NSManagedObjectModel*) managedObjectModel {
    @synchronized( self ) {
        if( !_managedObjectModel ) {
            NSLog(@"init _managedObjectModel");
            NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"Datamodel" withExtension:@"mom"];
            _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelURL];
        }
    }

    return _managedObjectModel;

}

And in @interface you have to declare 3 ivars and properties:

    NSManagedObjectContext*             _managedObjectContext;
    NSManagedObjectModel*               _managedObjectModel;
    NSPersistentStoreCoordinator*       _persistentStoreCoordinator;

@property(nonatomic, readonly) NSManagedObjectContext*          managedContext;
@property(nonatomic, readonly) NSManagedObjectModel*            managedObjectModel;
@property(nonatomic, readonly) NSPersistentStoreCoordinator*    persistentStoreCoordinator;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜