游戏重启后重新加载UIImage

时间:2014-02-17 07:33:04

标签: ios objective-c uiimage

我有一个使用照片库的游戏。你从图书馆挑选一张照片,然后你进入下一个屏幕,照片就在那里。你玩游戏,然后当它完成后你可以选择重新开始游戏。到目前为止,我已经做好了一切准备。我现在面临的问题是当游戏重新启动时,照片不会重新加载。我已经搜遍了所有的解决方案,但我找不到任何帮助我的东西。

所以基本上我需要一种方法让游戏保存照片,这样用户可以在重新启动后继续播放该照片。有没有人有任何想法我怎么能做到这一点?

注意:只要新照片被保存,如果用户决定选择其他照片,则可以覆盖照片。

感谢您的时间

修改

这是我的MenuViewController

    -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    self.myImage = [info objectForKey:UIImagePickerControllerEditedImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    [self performSegueWithIdentifier:@"game" sender:self];
    [self saveImage];

}

-(void) saveImage {

    NSData *pngData = UIImagePNGRepresentation(myImage);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
}

然后在我的ViewController中(为游戏加载图像

- (void)viewDidLoad
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];
[copter setImage:image];


}

在我的重启按钮上

 - (IBAction)refreshTheGame {


        UIStoryboard *storyboard = self.storyboard;
        ViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"GML"];
        svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentModalViewController:svc animated:YES];
        [self loadSavedImage];
    }

    -(void) loadSavedImage {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"];
        NSData *pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *image = [UIImage imageWithData:pngData];
        [copter setImage:image];
    }

2 个答案:

答案 0 :(得分:0)

当用户选择照片时,您可以将UIImage的数据保存到具有已知文件名的文件中,并在下次游戏重新启动时加载它。要将UIImage保存到文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"gameImage.png"];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];

再次加载它:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"gameImage.png"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];            
UIImage *img = [UIImage imageWithData:imageData];

或者,您可以将UIImage存储在游戏数据之外,以便下次可以再次加载它而无需点击文件系统。

答案 1 :(得分:0)

我弄清楚我做错了什么。我已使用工作代码编辑了上面的代码。