2012-04-06 5 views

Antwort

9

können Sie diesen Code verwenden


- (IBAction)addImage:(id)sender { 
    UIActionSheet *action = [[[UIActionSheet alloc] initWithTitle:@"Select image from" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From library",@"From camera", nil] autorelease]; 

    [action showInView:self.view]; 
} 

#pragma mark - ActionSheet delegates 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{   
     if(buttonIndex == 0) { 

      if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
       UIImagePickerController *pickerView =[[UIImagePickerController alloc]init]; 
       pickerView.allowsEditing = YES; 
       pickerView.delegate = self; 
       pickerView.sourceType = UIImagePickerControllerSourceTypeCamera; 
       [self presentViewController:pickerView animated:YES completion:nil]; 
      } 

     }else if(buttonIndex == 1) { 

      UIImagePickerController *pickerView = [[UIImagePickerController alloc] init]; 
      pickerView.allowsEditing = YES; 
      pickerView.delegate = self; 
      [pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
      [self presentViewController:pickerView animated:YES completion:nil]; 

     } 
} 

#pragma mark - PickerDelegates 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 

    [self dismissViewControllerAnimated:YES completion:nil]; 

    UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage]; 

    myImageView.image = img; 

} 
2
 1.Add this to Info.plist to access photos library. It is a new feature in ios10. 

    <key>NSPhotoLibraryUsageDescription</key> 
    <string>$(PRODUCT_NAME) uses photos</string> 

    2. - (IBAction)profilePicAction:(id)sender { 

     UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"" message:@"Change Profile image" preferredStyle:UIAlertControllerStyleActionSheet]; 

     UIAlertAction *takePhoto=[UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 

     picker.delegate = self; 

     picker.allowsEditing = YES; 

     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

     [self presentViewController:picker animated:YES completion:NULL]; 

     [alertController dismissViewControllerAnimated:YES completion:nil]; 
    }]; 
     [alertController addAction:takePhoto]; 

     UIAlertAction *choosePhoto=[UIAlertAction actionWithTitle:@"Select From Photos" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

     UIImagePickerController *pickerView = [[UIImagePickerController alloc] init]; 

     pickerView.allowsEditing = YES; 

     pickerView.delegate = self; 

     [pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 

     [self presentModalViewController:pickerView animated:YES]; 

     [alertController dismissViewControllerAnimated:YES completion:nil]; 
    }]; 

     [alertController addAction:choosePhoto]; 

     UIAlertAction *actionCancel=[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 


    [alertController dismissViewControllerAnimated:YES completion:nil]; 
    }]; 

    [alertController addAction:actionCancel]; 

    [self presentViewController:alertController animated:YES completion:nil]; 

    } 
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

     UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 

     self.profilePic.image = chosenImage; 

     [picker dismissViewControllerAnimated:YES completion:NULL]; 

    } 
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

     [picker dismissViewControllerAnimated:YES completion:NULL]; 

    } 
Verwandte Themen