Take picture and set it to UIImageView in iOS [duplicate] - iphone

This question already has answers here:
Taking a picture from the camera and show it in a UIImageView
(2 answers)
Closed 9 years ago.
Is there a way to take a picture on the iPad without going through the Apple controls ? I have seen a bunch of apps that do this,
For example, when you add a new contact in iPhone, on the top left side it shows add photo when we click on that, camera opens up and it takes pic and saves to add photo..
I want to implement same functionality.. is it possible on iPad ?

What you need to do is , Add a button first, with the title "Add Photo" or with custom image.
Then on the click of the button add the following code :
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController: cameraUI animated: YES completion:nil];
It will open up the camera, then you click the image and then tap "use" and You have to implement the UIImagePickerControllerDelegate method imagePickerController:didFinishPickingMediaWithInfo: and then store the UIImage to wherever you want, with whatever file name you want, using NSFileManager methods.

Using image picker controller you can use the same functionality as you described in your question.
For that you need to convert the picture into image data & display it in uiimageview
You can refer this : UIImageView Class Reference
This will provide you examples too.
This answer of Taking a picture from the camera and show it in a UIImageView will provide you proper answer..
Enjoy Programming!!

Related

UIImagePicker add Caption

I can't test this code in the iphone simulator but I was wondering if this is the best way to add a caption to a photo someone has taken in the UIImagepickercontroller class.
- (void)imagePickerController:(UIImagePickerController *) Picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
_selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
UITextField *caption= [[UITextField alloc] initWithFrame:CGRectMake( 60,640,200,40)];
caption.borderStyle = UITextBorderStyleRoundedRect;
caption.font = [UIFont systemFontOfSize:15];
caption.placeholder = #"Type Caption";
[caption release];
}
This is the flow I would take
Let user select / take a picture
Once user hits ok show a prompt to let them enter text.
Sample Project 1
Sample Projects 2
Create a UIImageView and add their selected picture to it
Create a UILabel and add it on top of the UIImageView
Add their text to UILabel.
If you need to save the image with text on top of it then you need some sort of custom photo editor code or just take a screenshot and save it.
The code you posted does not make much sense for what you are trying to do, since it does not look like you are actually linking the caption to the image in any way. I also do not know why you are releasing the text field - unless you turned Automatic Reference Counting off, that code should return an error.
What I would do (and did, to an extent, in a project of mine) is the following:
(1) Create some sort of object, like UserImg, which has two properties of type UIImage and NSString respectively.
(2) In the above function, when an image is selected, create a new UserImg and set the UIImage property to the select image.
(3) Display an UIAlertView with a text box and an okay and cancel button. Use the text box to get the caption you want the user to save with the image.
(4) Set up a function to listen to the UIAlertView for okay and cancel button presses. If the user cancels, remove the new UserImg. If the user hits okay, copy the input text to the UserImg's NSString property.
(5) Profit? I am not sure of the context of the image taking, so you'll have to figure out how you want to display things yourself.
You can test this functionality on the simulator as well. You just need to set the code up so that it picks images from the device's image folder if no camera is present. I forget exactly how to do this and do not currently have my source code on me. I'll edit the answer later to include that if you cannot figure it out yourself in that time.
You will also have to add images to the simulator to choose from. To do that, exit out of any apps running on the simulator (hit the home button, like you would on an actual device) and drag an image over on top of it. When you do, in the top right corner, there should be an option to save the image to the device.
EDIT:
I noticed the comment you added about putting the caption below the image. To do that, I would create a UserImgView which consisted of a UIImageView and a UILabel. That way, when displaying the image using the custom view, you can display the UIImage in its ImageView and the caption right below it in the label.
Best way would be to create a NSDictionary
NSMutableDictionary *photoCaptionDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_selected.image,#"image",caption.text,#"caption",nil];
Whenever you want to show the picture you just do
[photoCaptionDict objectForKey:#"image"];
and
[photoCaptionDict objectForKey:#"caption"];
shows the text.

How to make recent images on top of UIImagePickerController? [duplicate]

This question already has answers here:
UIImagepickercontroller: is it possible to change the sort order of the images in camera roll?
(3 answers)
Closed 9 years ago.
In my iPhone app, user can select photo from device while uploading,
When the click select photo-the UIImagePickerController comes. But its order is recent photos on bottom, so the user have to scroll a lot to select their recent pictures,How can I make it as recent pics on top of UIImagePickerController
With UIImagePickerController it is not possible. However you can overcome this by using ALAssetLibrary and create your controller.
If you are running short in time I will recommend you to use ELCImagePickerController, change the code of ElcAssetTablePicker.m preparePhotos method.
Add following code to it
if(self.tableView.contentSize.height > self.tableView.frame.size.height)
{
CGPoint offset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
[self.tableView setContentOffset:offset animated:NO];
}
And bingo you will see it will scroll to bottom of the table. So user can see recent photos directly.
Let me know in case of any query.
You can't do it.
There is no way to adjust the photolibrary data in UIImagePickerController.
If you need this you need to rely on any third party api's or you need to write your on picker which sorts images using their timestamps or asseturl's. (Currently no third party library is available for doing this)
We can do that
just use this in imagepicker
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

What are the methods/classes I need to create an iPhone upload photo form?

Currently I have a view with an ImageViewer and 2 buttons (Browse & Upload). I'm currently searching for methods to display the iPhone photo album once the button is pressed.
I've looked at this example here but I assume this one is the method you call when you want to upload the photo into the server.
Objective C: How to upload image and text using HTTP POST?
I'd like to duplicate the functionality of a web upload form.
1. Click button to browse folders.
2. Choose photo and display in a container.
3. Click Upload to upload photo into the server.
**Update:
Found some nice tutorials that helped me start with my iPhone app photo upload form.
Using UIImagePickerController
http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html
http://trailsinthesand.com/picking-images-with-the-iphone-sdk-uiimagepickercontroller/
Adding images to iPhone Simulator
You will have to use UIImagePickerController class
Here is the class reference for UIImagePickerController
Initialize the picker controller in your button event handler method.
Then set the UIImagePickerControllerDelegate to your current viewController
Here is the class reference for UIImagePickerControllerDelegate
picker.delegate = self;
Set the UIImagePickerControllerSourceType of picker to UIImagePickerControllerSourceTypePhotoLibrary
picker.UIImagePickerControllerSourceType = UIImagePickerControllerSourceTypePhotoLibrary
Then present the pickerViewController (if your target device is an iPad, present the imagePickerView in a popOver control)
Once user selects an image picker controller will automatically call its delegate method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
You can get the selected image by accessing the value for UIImagePickerControllerOriginalImage from the NSDictionary *info
Then you can use the image for uploading to server as mentioned in the post link
These are some tutorials for UIImagePickerController
Tutorial one
Tutorial two
Tutorial three
for getting images from iPhone library you have to use UIImagePickerController.
m_imagePickerController = [[UIImagePickerController alloc] init];
m_imagePickerController.delegate = self;
m_imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
hope this will help you

iPhone Xcode Camera Integration Tutorials [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I need some help. I need to integrate the camera into my app, and I want to learn about the following:
I need the camera button on my view so that clicking it opens the camera view.
I take a picture
I need to code so that I have access to the Phone Gallery and then
display a pic in another view.
Could anyone please point me in the right direction?
Well, UIImagePickerController is the tool you need. It will do most of the stuff in that checklist.
For the button you can create a custom button with graphics or if you are planning to use a tool bar or a navigation bar to hold your buttons, you can create the bar button using the UIBarButtonSystemItemCamera system item. This will give you the framework image.
On tapping it, you will create a UIImagePickerController instance and present it modally.
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[picker release];
As you must've noticed that it has a delegate property which is defined as id < UIImagePickerControllerDelegate, UINavigationControllerDelegate> delegate; so you will have to adopt both the protocols but in most cases you implement only two methods – imagePickerControllerDidCancel: and imagePickerController:didFinishPickingMediaWithInfo:. There is another method in UIImagePickerControllerDelegate protocol but that's deprecated. Don't use it even if you see it mentioned a lot around here. You would expect the cancel handler to be written like this,
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
The other methods is where you do most of the stuff.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
// You have the image. You can use this to present the image in the next view like you require in `#3`.
[self dismissModalViewControllerAnimated:YES];
}
Taking the picture is done automatically by the UIImagePickerController instance. However if you want to override their controls, you can do so by setting showsCameraControls to NO and then implementing your own cameraOverlayView. If you've done so and have assigned a button to take the picture, you can actually trigger the picture action using the takePicture method. So this should address #2.
You can use other properties to adjust your image picker too. For example, you can limit the user to only taking images using the mediaTypes property.
Paraphrasing the docs, dismissModalViewControllerAnimated: is deprecated from iOS6 onwards. Use dismissViewControllerAnimated:completion: instead.

iPhone: taking a picture programmatically

I'm trying to use the UIImagePickerController interface from OS 3.1, with the cameraOverlayView and takePicture, but I've clearly failed to understand how this works, and so I'm not getting the behaviour I want.
What I want to do is open the camera and take a picture automatically without having to having the user interact with the picker or edit the image. So I subclass UIImagePickerController (similar to the example in http://github.com/pmark/Helpful-iPhone-Utilities/tree/master/BTL%20Utilities/) and turn off all of the controls:
- (void)displayModalWithController:(UIViewController*)controller animated:(BOOL)animated {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.toolbarHidden = YES;
// Setting the overlay view up programmatically.
ipView = [[ImagePickerView alloc] init];
self.cameraOverlayView = ipView;
[controller presentModalViewController:self animated:NO];
}
In the overlayView, I've managed to force the takePicture method of UIImagePickerController to fire (I know this, because I can NSLog it, and I hear the sound of the camera taking a picture). The overlayView shows up just fine. However, the delegate method didFinishPickingMediaWithInfo: never gets called, and imagePickerControllerDidCancel doesn't get called either.
So, how do I either get the delegate methods to get called, or save the picture by overriding the takePicture method? (I have no idea how to capture the picture data here, and Google seems to have failed me). I can't help feeling that I've failed to understand how the guts of UIImagePickerController works, but the docs aren't overly helpful:
e.g.:
"You can provide a custom overlay view to display a custom picture-taking interface and you can initiate the taking of pictures from your code. Your custom overlay view can be displayed in addition to, or instead of, the default controls provided by the image picker interface."
or from showCameraControls:
"If you set this property to NO and provide your own custom controls, you can take multiple pictures before dismissing the image picker interface." - How do I dismiss the picker interface?
Note: the delegate is set properly in IB, so that's not the problem.
Thanks for any help you can provide!
I've found that you just have to wait "long enough" before calling takePicture, or it just silently fails. I don't have a good answer for how to determine the minimum value of "long enough" that will always work, but if you set a timer and wait five or ten seconds you should be okay. It would be nice if it returned some kind of an "I'm not ready to take a picture yet, sorry" error either directly from takePicture or through the delegate, but as far as I know it doesn't.
As an update to my own question: It turns out that I was trying to use takePicture too early. When I moved the action to a button on the overlay and sent takePicture from that button (once the picker was presented modally), the delegate methods fired as they should. I don't know if what I wanted is achievable - taking the image without having to press that button, automatically - but if it is, it will probably have to be done by sending takePicture sometime after I was trying to use it.
-(void)imageMethod:(id)sender{
imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePopover=[[UIPopoverController alloc]initWithContentViewController:imagePickerController];
[imagePopover presentPopoverFromRect:importButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}