2013-03-18 22 views
0

我在下面发布的代码有两个功能;第一个是拍一张照片(工作正常),第二个是从图书馆(这是不工作)的图像。这些函数没有正确调用。请检查我的代码,并让我知道最新的错误。从我的书架中添加照片时出错?

在此先感谢。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    static NSDateFormatter *dateFormatter = nil; 
    NSString *stringDate = nil; 
    if (dateFormatter == nil) { 
     dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"EE, d LLLL yyyy"]; 
     stringDate = [dateFormatter stringFromDate:[NSDate date]]; 
    } 
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     // storing to camera roll 
     UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:finishedSavingWithError:contextInfo:),nil); 
     CGSize newSize=CGSizeMake(320, 436); 
     UIGraphicsBeginImageContext(newSize); 
     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     xapp.profileImage=newImage; 
     [[NSNotificationCenter defaultCenter]postNotificationName:@"addImage" object:nil]; 
    } 

    // Commit the change. 
    [self dismissModalViewControllerAnimated:YES]; 
} 

-(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 
    if (error) { 
     UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle: @"Save failed" 
          message: @"Failed to save image" 
          delegate: nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 
+0

以什么方式是从库代码挑选不工作?它会产生错误吗?拾取器是否出现? – gaige 2013-03-18 12:38:14

+0

第二个函数在UIImagePickerController运行时发生错误时调用,而不是从库中加载照片时调用。你能对这个错误有更详细的了解吗?你是如何调用UIImagePickerController从库中加载的? – 2013-03-18 12:39:12

+0

图书馆是开放的罚款如果选择一个图像,图像不显示在imageView – 2013-03-18 12:46:54

回答

1

您好我完全捕获图像从照片库中的摄像头只是把代码只是检查出来,并尝试impliment在您的项目: -

-(IBAction)actionImage:(id)sender 
{ 
    UIActionSheet *option =[[UIActionSheet alloc]initWithTitle:@"Select" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select Photo",@"Take Photo",nil]; 
    option.actionSheetStyle =UIActionSheetStyleDefault; 
    [option showInView:self.view]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (actionSheet.tag==2) 
    { 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
    else 
    { 

     UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 

     if(buttonIndex ==0) 
     { 
      if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) 
      {  
       picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
       [self presentModalViewController:picker animated:YES];  
      } 
     } 
     else if(buttonIndex ==1) 
     { 
      if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
      {  
       picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
       [self presentModalViewController:picker animated:YES];  
      } 
     } 
    } 
} 
#pragma mark - imagePickerController Delegate 
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
    imgUserImage.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];  

    return; 
} 

ñ OTE: - 不要忘记包含相关的框架或委托。 希望它的帮助,你不需要额外的编码这个东西,你只是简单地用这个上面的方法:)

+0

库开放罚款如果选择一个图像,图像不显示在imageView – 2013-03-18 12:48:39

+0

检查IBOutLet连接从笔尖(XIB)或不:) – 2013-03-18 12:49:13

0

,我们在您需要code..Here许​​多变化是从library..I采摘的图像的代码想不如给你,而纠正错误yours..so代码,这里是代码..enjoy ...

-(void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSString *mediaType = info[UIImagePickerControllerMediaType]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
     UIImage *image = info[UIImagePickerControllerOriginalImage]; 

     _imageView.image = image; 
     if (_newMedia) 
      UIImageWriteToSavedPhotosAlbum(image, 
       self, 
       @selector(image:finishedSavingWithError:contextInfo:), 
       nil); 
     } 
     else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) 
     { 
       // Code here to support video if enabled 
     } 
} 

-(void)image:(UIImage *)image 
finishedSavingWithError:(NSError *)error 
contextInfo:(void *)contextInfo 
{ 
    if (error) { 
     UIAlertView *alert = [[UIAlertView alloc] 
      initWithTitle: @"Save failed" 
      message: @"Failed to save image" 
      delegate: nil 
      cancelButtonTitle:@"OK" 
      otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

对不起相同的问题 – 2013-03-18 12:45:36

+0

图书馆开放罚款如果选择一个图像,图像不显示在图像查看 – 2013-03-18 12:48:33

+0

@ IOS234你试试这个代码它为我工作..也认为适合你...如果你想任何进一步的援助答复.. – Shivaay 2013-03-19 05:34:23