我正在制作游戏,我想从相机胶卷中选择游戏场景的背景图像。
我拥有从ViewController.m中的Camera Roll导入imageView的所有必要代码,但它在Game SKScene中不起作用。
我尝试在SKScene中导入您从ImagePicker中选择的ImageView,然后将其设置为背景图像,但是当我构建它时应用程序崩溃。
提前致谢。
答案 0 :(得分:0)
我不太确定你在这里尝试做什么,但是你想从相机胶卷中获取照片并将其作为背景吗?
@interface ViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
IBOutlet UIImageView *ImageView;
UIImagePickerController *picker;
UIImage *image;
}
//.m file
-(IBAction)TakePhoto:(id)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//Saving the image
NSString *imgName= @"imgname.png";
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[BackGround setImage:image]; //Setting the background to the image
if (image.size.width < image.size.height) {
BackGround.contentMode = UIViewContentModeScaleAspectFill; //If the image too big make it fit
BackGround.clipsToBounds = YES;
//Re-save the image
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *imageSave = image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(imageSave);
[imageData writeToFile:savedImagePath atomically:NO];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
//Collect the image, pass it between files/view controllers
NSString *imgName= [[NSUserDefaults standardUserDefaults]valueForKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,imgName];
[BackGround setImage:[UIImage imageWithContentsOfFile:filePath]];