I am trying to save the contents of UIImageView into a variable that I can send to a server.
The contents of UIImageView is selected by the user from their camera role.
I cant seem to actually store the binary data / other format to send, how can I achieve this? Everything I have found on the net is saving the image into main memory which isnt what I want.
Edit:
Here is the actual image selection:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
If you want to send image to server. The only way to send it is by converting the UIImage to NSData .
NSData *imageData= UIImageJPEGRepresentation(image,0.0);
NSData *imageData = UIImagePNGRepresentation(image);
Related
I have built a sample project on camera and library. Now, my question is can I store the images that I am selecting from library into an array? I am using this code.
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.arrImages addObject:chosenImage];
Now by placing breakpoints, I came to know that, in my array always 1object is storing even after I chosen images for more than one times.
Can anyone give my any idea how can I do it?
Any help is highly appreciated.
Try this.
-(void)viewDidLoad {
//declare before NSMutableArray *_mutableArray;
_mutableArray = [[NSMutableArray alloc] init];
}
....
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//dismiss UIImagePickerController
[picker dismissViewControllerAnimated:YES completion:Nil];
//take image from anywhere
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//store image into NSMutableArray
[_mutableArray addObject:image];
}
I am using Image picker Controller to take pictures and save them into database. I have this method to to get the image taken by the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
This method is getting called automatically as it is delegated as self. I tried to call this ,when I click a button,but I couldn't get the image . It is showing me null.Here is the method I am calling the picker controller delegate method.
- (IBAction)save_Image:(id)sender {
UIImagePickerController *saveDelegate;
NSDictionary *infoDelegate;
[self imagePickerController:(saveDelegate) didFinishPickingMediaWithInfo:(infoDelegate)];
UIImage *chosenImage = infoDelegate[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
//here i couldn't get the image. I am getting null
}
Is there any possible way that I can get the image by calling that method from my own method?
If you want to inform others of when a user picks an image, this is a perfect place to use notifications.
static NSString* const ImagePickerDidFinishPickingPhotoNotification = #"ImagePickerDidFinishPickingPhotoNotification";
#implementation MyViewController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[[NSNotificationCenter defaultCenter] postNotificationName:ImagePickerDidFinishPickingPhotoNotification
object:picker
userInfo:info];
}
#end
I'm pretty new in this so don't be harsh on me.
I have a web page that when a button is clicked, it does a change of URL, this is detected by Objective-C and opens the Camera.
Until here fine.
Now I want to return the image to the webview but I cant.
I'm trying to run a JavaScript to do it as it says the guide
When the image is taken and used, this method runs:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//self.imageView.image = chosenImage;
id win = [_webView windowScriptObject];
[win evaluateWebScript: #"addImage(’sample_graphic.jpg’, ‘320’, ‘240’)"]; //HERE instead of the string, I want to send 'chosenImage'
[picker dismissViewControllerAnimated:YES completion:NULL];
}
But I get the errors:
No visible #interface for UIWebView declares selector 'windowScriptObject'
Which I don't really know what it means.
Have to say that this is coded in the WebViewController, which is my only and root controller.
I am using static librrary and there is one class inside it that uses UIImagePickerViewController. I have set the delegate to it and written didFinishPickingMediaWithInfo: method within that class. What I want is to open front camera, capture the image when user clicks it and then save it to Photo Albums. What is happening with my code is that UIImagePickerController loads the camera and it saves the photo after capturing when I click on "Use Photo" button. But after that, it freezes but not crashes. I can't click anywhere after that. Any help appreciated. Thanks
// CODE IN LIBRARY
+ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* photo = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(photo);
photo = [UIImage imageWithCGImage:[UIImage imageWithData:data].CGImage
scale:photo.scale
orientation:photo.imageOrientation];
UIImageWriteToSavedPhotosAlbum(photo, self, nil, nil);
// TRIED ALL OF THE BELOW BUT NOTHING WORKS FOR ME
[picker dismissViewControllerAnimated:NO completion:nil];
// [picker dismissModalViewControllerAnimated:YES];
[picker.view.superview removeFromSuperview];
// [picker release];
// picker=nil;
}
Your method shouldn't be static - remove the '+' and replace it with '-'.
Also, in the method you do
[picker.view.superview removeFromSuperview];
I'm not sure that's what you want to do !
So your code becomes :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
...
}
I am displaying html file in UIWebView which contains file input field. When I click on file input field, it opens default UIImagePicker. So I need image selected by that UIIMagePicker in iOS.
Please help me.
Thanks
try to use this webview delegate method.i hope so it will help you.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
//write your image picker code.
}
You can load image from photolibrary to webview as shown in below
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *urlPath = [info valueForKey:UIImagePickerControllerReferenceURL];
UIImage *cameraImage = [info valueForKey:UIImagePickerControllerOriginalImage];
NSData *myData = UIImagePNGRepresentation(cameraImage);
[self.webview loadData:myData MIMEType:#"image/png" textEncodingName:nil baseURL:nil];
[self.picker dismissViewControllerAnimated:YES completion:nil];
}