[__ NSPlaceholderArray initWithObjects:count:]:尝试从对象[0]插入nil对象

时间:2014-06-16 06:14:25

标签: ios uiimage nsarray nsuserdefaults uiactivityviewcontroller

我有EAGLView hodling presentFrameBuffer并保存来自EAGLView的屏幕截图将此sreenshot发送到UIViewController以获取UIActivityViewController社交网络框架。所以,我保存在NSUserDefaults然后在UIViewController中检索。但由于未被捕获的异常而导致终止应用程序' NSInvalidArgumentException',原因:'*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

EAGLView.h

@property (nonatomic, copy) UIImage * screensht;

   @property (nonatomic, copy) UIButton * save;

EAGLview.mm

- (BOOL)presentFramebuffer
 {


 if (_takePhotoFlag)

 {


 UIImage *glImage = [self glToUIImage];
 self.screensht = [self createSavableImage:glImage];

  [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(self.screensht) forKey:@"image"];

 UIImageWriteToSavedPhotosAlbum(self.screensht, nil, nil, nil);

 }

 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);

 return [context presentRenderbuffer:GL_RENDERBUFFER];
 }

 -(void)save:(id)sender{


 _takePhotoFlag = YES;

 }

Viewcontroller.mm:

- (void)viewDidLoad
 {
 [super viewDidLoad];

 [eaglView.save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];

 }



 -(void)save:(id)sender{


 NSLog(@"save click");

 NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
 UIImage* image = [UIImage imageWithData:imageData];



 NSArray *activityItems = @[image];

 UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
 avc.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, nil];
 [self presentViewController:avc animated:YES completion:nil];



 }

NSUserDefaults Null:

 [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(self.screensht) forKey:@"image"];

         NSLog(@"nsuserdefauls is %@",[[NSUserDefaults standardUserDefaults] stringForKey:@"image"]);

nsuserdefauls is (null)

2 个答案:

答案 0 :(得分:0)

您将图像存储为NSData对象,但尝试使用stringForKey检索它 - 这将使您为零,因为“image”键不包含字符串。你应该使用

NSData *imageData=[[NSUserDefaults standardUserDefaults] objectForKey:@"image"]);
UIImage *image=[UIImage imageWithData:imageData];

答案 1 :(得分:-1)

对我来说答案是我正在打电话

if([_something respondsToSelector:@selector(objectForKey:)])
{
   id a = [_something objectForKey:@"someKey"];
}

但是' _something'是' nil'那就是我遇到崩溃的地方