3

I have beginner of iPhone any source code and suggestion related display image in to our apps and pick the photo from gallery

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
  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 に答える