I currently have a UIImageView and a UIImage. How do I allow the user to select one of their photos from the camera roll?
There are possible duplicates, but I've had no luck. This might possibly be because I don't hook things up in the Interface Builder.
I would like something simple like UIImage * image = [UIImage imageFromCameraRoll], but unfortunately, that's not possible (I don't think so, at least).
Try this code
It is help to you get image from cameraroll & photolibrary. using UIImagePickerViewController
if you take Picture
From camera (in both open As UINavigationcontroller).
From Gallery(for ipad open as UIpopovercontroller & for iphone open as nvigationcontroller).
First set delegate
#interface camera : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate>
get image from Camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = YES;
imagePicker.wantsFullScreenLayout = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
newMedia = YES;
iscamera = 0;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error to access Camera"
message:#""
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
Get image from Gallery
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *_picker=nil;
if (popoverController) {
[popoverController dismissPopoverAnimated:NO];
}
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_picker.wantsFullScreenLayout = YES;
//[popoverController presentPopoverFromBarButtonItem:sender
// permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self presentViewController:_picker animated:YES completion:nil];
} else
{
popoverController = [[UIPopoverController alloc] initWithContentViewController:_picker];
[popoverController setDelegate:self];
[popoverController presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
}
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error access photo library"
message:#"your device non support photo library"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
Both Result you get in Delegate Method
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// write your code here ........
}
Use UIImagePickerController, see the Apple docs.
Select the source type:
#property (nonatomic) UIImagePickerControllerSourceType sourceType
From on on these:
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
Related
In my camera app i want when i click an image it will ask for title and description from the user and then save it.right now its saving images but not asking for title and description.I am new to iOS.so,may be this is a silly question.please help me to get this.for camera click i am using this method:-
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
BOOL camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if(camera)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[[picker navigationBar]setBarStyle:UIBarStyleDefault];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:NULL];
[picker release];
}
You can do it in the following way:
Click the image using camera.
Come back to your app without saving the image. You'll have a reference of the image clicked in your application till now.
ask the user for the title & description.
Save the image to picture library using this line of code:
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void contextInfo);
Hope it helps you.
More Help Here....
For me this tutorial guided me to achieve what i want.So, i think to share the link.May be someone gets benefited.
http://www.raywenderlich.com/13541/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-22
and this piece of code helps me out..
-(void) takePhoto{
BOOL camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if(camera)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[[picker navigationBar]setBarStyle:UIBarStyleDefault];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:NULL];
[picker release];
actionSheetbool = false;
}
else
{
UIAlertView* cameraAlert = [[UIAlertView alloc]initWithTitle:#"Sorry!!" message:#"Camera not found." delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[cameraAlert show];
[cameraAlert release];
}
}
-(void)photoLibrary{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
[imagePicker release];
actionSheetbool = false;
}
}
Hi all i'm new iphone developer,
I have problem while open the uiimagepickercontroller.
I have option take the photo and open gallery in popoverview to take the photo and open the camera that time hide the popoverview and open gallery hide the popover and open the imagepicker from parent view controller imagepicker should not open from popover view controller.
please share your ideas.
// For opening camera
-(void)btnCameraClicked {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Camera is not Available" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alert show];
[alert release];
}
}
// For opening image picker controller
-(void)btnGalleryClicked {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imgPicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"Photos are not available" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alert show];
[alert release];
}
}
After this you can use ImagePicking Methods like :
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
You can use UIPopoverControllerDelegate method
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController;
I implement UIImagePickerController delegate, UINavigationController delegate, UIPopOverController delegate. I have no clue what the problem is. My device restarts after calling this 3, 4 or 5 times ( It is different each build ). Please help me fix it!!
EDIT: I get this error:
More than maximum 5 filtered album lists trying to register. This will fail.
Here is the code I am using to call UIImagePickerController and get the image:
- (IBAction)imgPickerPressed:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
imgPickerTypeActionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose image source:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Photo Library", nil];
[imgPickerTypeActionSheet showInView:self];
}
else {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setDelegate:self];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
imgPickerPopOver = [[UIPopoverController alloc] initWithContentViewController:controller];
imgPickerPopOver.delegate = self;
[imgPickerPopOver presentPopoverFromRect:CGRectMake(imgPickerButton.frame.origin.x, imgPickerButton.frame.origin.x-250, 0.0, 0.0)
inView:self
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([actionSheet isEqual:imgPickerTypeActionSheet]) {
if (buttonIndex == 0) {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setDelegate:self];
[controller setSourceType:UIImagePickerControllerSourceTypeCamera];
[[delegate getVC] presentModalViewController:controller animated:YES];
}
if (buttonIndex == 1) {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setDelegate:self];
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
imgPickerPopOver = [[UIPopoverController alloc] initWithContentViewController:controller];
imgPickerPopOver.delegate = self;
[imgPickerPopOver presentPopoverFromRect:CGRectMake(imgPickerButton.frame.origin.x, imgPickerButton.frame.origin.x-250, 1, 1)
inView:self
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[imgPickerPopOver dismissPopoverAnimated:YES];
pickedImageView.image = image;
[self valueChanged:nil];
}
Does this work in the iOS simulator? Also are you releasing any of these objects, or is ARC doing it? This may help.
How can we display the UiImagepicker controller interface with both camera and video mode and also with Photo Library icon button,same as default camera App for iPhone.
or How to remove cancel Button (shown in Camera view) and replace with different button. Is it possible and if will apple approve this approach.
Please help me out ??
You can try this way.
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if(hasCamera){
UIActionSheet *actionSheet;
actionSheet = [[[UIActionSheet alloc] initWithTitle:#"Add Photo"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Select from Library", #"Take a New Photo", nil] autorelease];
actionSheet.actionSheetStyle = UIBarStyleBlackOpaque;
[actionSheet showInView:[self view]];
}
else {
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
Actionsheet delegate method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
if(buttonIndex == 0)
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
else if(buttonIndex == 1)
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
Image picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
if(image)
{
[self.addPhotoButton setBackgroundImage:image forState:UIControlStateNormal];
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
I'm trying to create an app that will let you import photos from your Photo Library and insert them into designated UIImageViews. I was able to get this to work for one of the UIImageViews in my interface, but can't get any to assign to the second UIImageView.
If anyone can tell me what I'm doing wrong I would greatly appreciate it! Thanks! SBB
Here is my code:
#implementation FlipBook2ViewController
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing photo library"
message:#"Device does not support a photo library"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (IBAction)selectExistingPicture2 {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing photo library"
message:#"Device does not support a photo library"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
Your imageView in the delegate method imagePickerController:didFinishPickingImage:editingInfo: isn't changing. You should probably have an instance variable for the current image view, say something like currentImageView and them implement the delegate method like,
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
currentImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
For this to work however you will have to alter your selectExistingPicture a bit.
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
currentImageView = imageView1; // Set it to the image view that will get changed
[..]
}
else {
[..]
}
}
However you will have to do this for every method. I am not sure what the exact trigger for this method is but it would be appropriate to have the sender argument in the action method to avoid repetition.
- (IBAction)selectExistingPicture:(id)sender {
switch(sender.tag) {
/* set `currentImageView` based on the tag */
}
/* Rest of your method from the question remains the same */
}