Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>the following code uses a button on a toolbar, when pressed it will bring up the camera roll and then u will be able to pick the image to set</p> <p>Start by adding the MobileCoreServices framework</p> <p>add the following code in the .h file </p> <pre><code> #import &lt;UIKit/UIKit.h&gt; #import &lt;MobileCoreServices/MobileCoreServices.h&gt; @interface cameraViewController : UIViewController &lt;UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate&gt; @property (nonatomic) BOOL newMedia; @property (nonatomic, strong) IBOutlet UIImageView *imageView; @property (nonatomic, strong) UIPopoverController *popoverController; @property (nonatomic, strong) IBOutlet UIToolbar *toolbar; - (IBAction)useCameraRoll: (id)sender; @end </code></pre> <p>Add the following code in the .m file:</p> <pre><code>@synthesize imageView, popoverController, toolbar, newMedia; - (IBAction) useCameraRoll: (id)sender { if ([self.popoverController isPopoverVisible]) { [self.popoverController dismissPopoverAnimated:YES]; } else { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeImage, nil]; imagePicker.allowsEditing = NO; self.popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker]; self.popoverController.delegate = self; [self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; newMedia = NO; } } } #pragma mark - #pragma mark UIImagePickerControllerDelegate -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [self.popoverController dismissPopoverAnimated:true]; NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; [self dismissModalViewControllerAnimated:YES]; if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; self.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 dismissModalViewControllerAnimated:YES]; } </code></pre> <p>Add the following in viewDidUnload:</p> <pre><code>- (void)viewDidUnload { self.imageView = nil; self.popoverController = nil; self.toolbar = nil; } </code></pre> <p>Dont forget to connect the imageview to your image and the cameraroll button to your button.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload