Here is the code. It has takePhoto method for loading camera. selectPhoto for CameraRoll.
When I am trying to select photo from camera roll it works well and i can upload the image.
While I am trying to take photo from camera and upload than the app crashes.
- (void)takePhoto {
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
}else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)selectPhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//picker.modalPresentationStyle = UIModalPresentationCurrentContext;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
chosenImage = info[UIImagePickerControllerEditedImage];
self.imgUser.image = chosenImage;
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
selectedFileName = [representation filename];
NSLog(#"fileName : %#",selectedFileName);
encodedImage = [UIImageJPEGRepresentation(chosenImage, 0.8) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//NSLog(#"Encoded Image : %#", encodedImage);
NSString *newImage;
NSString *newImage1;
newImage = [encodedImage stringByReplacingOccurrencesOfString:#"+"
withString:#"%2B"];
newImage = [newImage stringByReplacingOccurrencesOfString:#"/"
withString:#"%2F"];
newImage1 = [newImage stringByReplacingOccurrencesOfString:#"="
withString:#"%3D"];
//NSLog(#"Encoded Image : %#", newImage1);
NSLog(#"Selected File Name : %#", selectedFileName);
//Sending Upload Request
[activityIndicator startAnimating];
[[MyService MySingleton] sendRequestWithHeader:_headerString param:[NSArray arrayWithObjects:newImage1,selectedFileName, nil] msg:#"uploadPhoto"];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Getting Following thing :
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Also getting image name as null when taking a picture and trying to access it in didFinishPickingMediaWithInfo method.
I have solved the issue as image was coming as null. With the following code :
if (representation.filename!=nil) {
selectedFileName = [representation filename];
}else{
selectedFileName = #"user.jpeg";
}
Related
I have created two options in the screen.
To capture the image from camera using : UIImagePickerControllerSourceTypeCamera
To load the image from the UIImagePickerControllerSourceTypeSavedPhotosAlbum.
But when i am capturing the image, image is not storing in the device?
My code is:
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"From didDismissWithButtonIndex - Selected Option: %#", [actionSheet buttonTitleAtIndex:buttonIndex]);
NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Camera"]) {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:#"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
return;
}
else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Gallery"])
{
UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
[pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
//[pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerView animated:YES completion:nil];
}
}
#pragma mark - PickerDelegates
//=====================================Image picker ===============================================
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex] ;
UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
tableIMAGE.image=orginalImage;
imageStris = [UIImageJPEGRepresentation(tableIMAGE.image,1)base64Encoding];
answersARRAY[indexPath.row] = [NSString stringWithFormat:#"-1,%#,%#",answersARRAY[indexPath.row],imageStris];
[self visubull];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Just put this line in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ to store image
When you capture image you have to store it manually.
UIImageWriteToSavedPhotosAlbum(orginalImage,nil, nil, nil);
Hope this will help you
It's because you're not doing anything with the image. You assign it as b64 string to an array and that's it. You must save it to the camera roll manually:
UIImageWriteToSavedPhotosAlbum(tableIMAGE.image, nil, nil, nil);
You have to save image camera roll in didFinishPickingMediaWithInfo like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
if(picker.sourceType==UIImagePickerControllerSourceTypeCamera)
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:nil];
}
}
I am try to develop iOS app which get the location of the Photo using UIImagePickerController and display it I am deploying it on iOS 7 device Here is what i did .
- (IBAction)takePhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
picker.showsCameraControls = YES;
[self presentViewController:picker animated:YES
completion:^ {
// [picker takePicture];
}];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
// Get the location property from the asset
CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
// I found that the easiest way is to send the location to another method
latitude =location.coordinate.latitude; //[[gpsdata valueForKey:#"Latitude"]floatValue];
longitutde =location.coordinate.longitude;
NSString * strLocation=[NSString stringWithFormat:#"La:%f Lo%f",latitude,longitutde];
};
// This block will handle errors:
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"Can not get asset - %#",[myerror localizedDescription]);
// Do something to handle the error
};
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(#"Image:%#", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
I am not able to get .How to get the location ? please help me on this .
in the delegate implementation you can use the path of the image or the image itself:
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
Location Services Should be enabled when the photo was taken.Try same code by enabling location services.
I have a photo picker in my app that asks the user if they want to take a picture or choose one from the photo library. Upon completion of their choice I set an image view to their chosen image and I want to save the image they just set only if it was taken from the camera and not if it was just an edited version of a photo they already have. The last method listed here is the one in which I wish to save the photo.
- (IBAction)startPicker:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIActionSheet *picChoice = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Photo Library", nil];
[picChoice showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
} else {
UIActionSheet *picChoice = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Photo Library", nil];
[picChoice showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Camera"]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Photo Library"]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
return;
}
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:editedImage.CGImage orientation:(ALAssetOrientation)editedImage.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
{
NSLog(#"IMAGE SAVED TO PHOTO ALBUM");
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
{
NSLog(#"we have our ALAsset!");
}
failureBlock:^(NSError *error )
{
NSLog(#"Error loading asset");
}];
}];
Simply check the sourceType:
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// save to library
}
Do this in your imagePickerController:didFinishPickingMediaWithInfo: method.
I Would create a app with camera but when i write this code in ImageViewController.m
xcode: EXPECTED METODY BODY and "."
how i can fix this?
thanks
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
It says you have not method at all. Try changing to this (note the braces after method name and after all code):
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate{
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
}
Can You Try this.
- (IBAction)presentPicker:(id)sender {
// **********************************************
// * Show action sheet that will allow image selection from camera or gallery
// **********************************************
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Image from Camera", #"Image from Gallery", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
#pragma mark - Image picker methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
switch (actionSheet.tag) {
case 1:
switch (buttonIndex) {
case 0:
[self showCameraImagePicker];
break;
case 1:
[self showGalleryImagePicker];
break;
}
break;
default:
break;
}
}
- (void)showCameraImagePicker {
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Simulator" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
#endif
}
- (void)showGalleryImagePicker {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:YES completion:NULL];
photo.image = image;
//[self presentImageCropperWithImage:image];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:NO completion:nil];
// Extract image from the picker
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
photo.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
// [self presentImageCropperWithImage:image];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I have a problem, after record video, I click on Use button to save this video to library but after save done, I can not tap on Play button or Retake button. This is my code :
-(void)actionLaunchAppCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.showsCameraControls = YES;
imagePickerController.allowsEditing = YES;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
imagePickerController.delegate = self;
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self presentModalViewController:imagePickerController animated:YES];
//[imagePickerController release];
}
}
}
saving the image that was taken
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
completionBlock:^(NSURL *assetURL, NSError *error){
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
NSDate* date = [asset valueForProperty:ALAssetPropertyDate];
NSLog(#"Date Time Modify %#",date);
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: #"dd.MM.yyyy HH:mm:ss"];
NSString* CurrentDateStamp = [formatter stringFromDate:date];
NSLog(#"NameVideo %#",asset.defaultRepresentation.filename);
NSLog(#"DateModify %#",CurrentDateStamp);
NSLog(#"Size: %lld", asset.defaultRepresentation.size);
} failureBlock:nil];
actionSheetUpload= [[UIActionSheet alloc] initWithTitle:#"Do you want to upload this video?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Upload" otherButtonTitles:nil];
actionSheetUpload.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheetUpload showInView:[UIApplication sharedApplication].keyWindow];
[actionSheetUpload release];
}
];
}
[library release];
}
Any ideas? Thanks in advance
Maybe you not dismiss picker with - (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion
The Code is just fine for saving the video, If you want to get it play. Then You need to use player to get it play.
For Example:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
.// Save Your Stuff
.
.
MPMoviePlayerController *movieplayer =[[MPMoviePlayerController alloc] initWithContentURL: recordedVideoURL];
[[movieplayer view] setFrame:
[self.view bounds]];
[self.view addSubview:
[movieplayer view]];
[movieplayer play];
}
Edit:
For Uploading a Video, According to your code :
Declare the NSURL *recordedVideoURL;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//NSLog(#"but ind %d", buttonIndex);
if (buttonIndex == uploadBTnIndex) {
NSData *videoData = [NSData dataWithContentsOfURL:recordedVideoURL];
//Use this NSData to upload, So what else your problem to upload using web Services ?
}