简体   繁体   English

从我的照片库添加照片时出错?

[英]Error in adding a photo from my library?

The code I have posted below has two functions; 我在下面发布的代码具有两个功能: the first one is to take a photo (which is working fine) and the second one is for picking an image from the library (which is not working). 第一个是拍照(工作正常),第二个是从图库中拾取图像(工作不正常)。 The functions are not getting called properly. 这些函数未正确调用。 Please check my code and let me know whats wrong. 请检查我的代码,让我知道出了什么问题。

Thanks in advance. 提前致谢。

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

hi i just put code of fully capture Image From Camera of Photo library just check it out and try to impliment In your Project:- 嗨,我只是将从相机库的相机中完全捕获图像的代码放出来,然后尝试将其隐含在您的项目中:

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

NOTE:- DO not forget Include related Framework or delegate. 注意:-不要忘记包括相关的框架或委托。 Hope its help's you no need extra coding for this stuff you just simply do with this above method :) 希望它的帮助是您只需要使用上述方法即可完成此工作:

There are many changes needed in your code..Here is the code for picking an image from library..I think better to give you the code rather correcting errors in yours..so, here is the code..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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM