【问题标题】:Error in adding a photo from my library?从我的照片库添加照片时出错?
【发布时间】:2013-03-18 12:14:30
【问题描述】:

我在下面发布的代码有两个功能;第一个是拍照(工作正常),第二个是从库中挑选图像(不工作)。这些函数没有被正确调用。请检查我的代码,让我知道有什么问题。

提前致谢。

-(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];
    }
}

【问题讨论】:

  • 从库代码中挑选不工作的原因是什么?它会产生错误吗?拣货员上来了吗?
  • 第二个函数在 UIImagePickerController 遇到错误时调用,而不是从库中加载照片时调用。你能更具体地了解一下这个错误吗?你是如何调用 UIImagePickerController 从库中加载的?
  • 库打开正常如果选择图像,图像不会显示在imageView中

标签: iphone ios objective-c xcode photolibrary


【解决方案1】:

你好,我只是把从照片库的相机中完全捕获图像的代码检查出来并尝试在你的项目中实现:-

-(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;
}

注意:- 不要忘记包含相关的框架或委托。 希望它对您有所帮助,您只需使用上述方法即可:)

【讨论】:

  • 库打开正常如果选择图像,图像不会显示在imageView中
  • 检查 IBOutLet 是否从 Nib(XIB) 连接:)
【解决方案2】:

您的代码需要进行许多更改..这是从库中选择图像的代码..我认为最好给您代码而不是更正您的代码中的错误..所以,这里是代码..享受。 ..

-(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];
}

【讨论】:

  • 库打开正常如果选择图像,图像不会显示在imageView中
  • @IOS234 你试试这个代码它对我有用..并且认为对你也有用......如果你需要任何进一步的帮助回复..
猜你喜欢
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多