3

我在我们的应用程序中有任何源代码和建议相关显示图像的 iPhone 初学者,并从图库中挑选照片

4

3 回答 3

15

你可以使用这个代码

- (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;
}
于 2012-04-06T11:57:24.190 回答
2

该功能称为UIImagePickerController

检查此以获取更多详细信息:https ://zcentric.com/2008/08/28/using-a-uiimagepickercontroller/

于 2012-04-06T11:44:08.357 回答
2
  1. 将此添加到 Info.plist 以访问照片库。这是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];
    }
    
于 2016-10-26T09:09:50.633 回答