I have beginner of iPhone any source code and suggestion related display image in to our apps and pick the photo from gallery
3 Answers
you can use this code
- (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;
}
-
The clickedButtonAtIndex function in this example has the two back to front. Clicking "From library" brings up the camera, and clicking "From camera" brings up the library. You also need to add two string values to your pList to use this example: NSLocationWhenInUseUsageDescription and NSPhotoLibraryUsageDescription.– Anthony.Jun 11, 2019 at 12:57
The functionality is known as UIImagePickerController.
Check this for more details: https://zcentric.com/2008/08/28/using-a-uiimagepickercontroller/
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>
- (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]; }