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 ?
}
Related
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";
}
I'm developing an app where user can record video and save it in the library/or custom album. I'm able to create the custom album and record the video and save it in the default photo library i.e., camera roll.. But i'm unable to save it to the custom album. and other thing is the saved videos in the album must be showed in the collection view of the app i.e., like the gallery view.. so that user can able to click on the videos for the play.. it might be in the grid view/ collection view or might be in the table view where the UIImage should should display the videos saved in the album..
Here's code section which i used in the app development for creating the custom album.
ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init];
[libraryFolder addAssetsGroupAlbumWithName:#"HEP" resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"Adding Folder:'My Album', success: %s", group.editable ? "Success" : "Already created: Not Success");
} failureBlock:^(NSError *error)
{
NSLog(#"Error: Adding on Folder");
}];
For recording the video in the app.
- (IBAction)StartRecord:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
For saving it in the album i.e., default photo album not the custom album i created.
-(BOOL)startMediaBrowserFromViewController:(UIViewController*)controller usingDelegate:(id )delegate {
// 1 - Validations
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
// 2 - Get image picker
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = YES;
mediaUI.delegate = delegate;
// 3 - Display image picker
[controller presentModalViewController:mediaUI animated:YES];
return YES;
}
Since i'm beginner in iOS still learning.. I'm searching since for a month now. So please help me.
First create your album folder with below code & make sure this code paste in AppDelegate.m file didFinishLaunchingWithOptions method :
ALAssetsLibrary* libraryFolderSozialConnectVideo = [[ALAssetsLibrary alloc] init];
[libraryFolderSozialConnectVideo addAssetsGroupAlbumWithName:#"HEP" resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"HEP Folder Created");
} failureBlock:^(NSError *error) {
NSLog(#"Error: Adding on Folder");
}];
Now, use below function for download your video in HEP folder :
- (void)downloadVideo:(NSURL *)videoURL {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSData *urlData = [NSData dataWithContentsOfURL:videoURL];
if (urlData) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"thefile.mp4"];
[urlData writeToFile:filePath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.library saveVideo:[NSURL fileURLWithPath:filePath] toAlbum:#"HEP" completion:^(NSURL *assetURL, NSError *error)
{
NSLog(#"Success downloaded");
} failure:^(NSError *error) {
NSLog(#"Error : %#", [error localizedDescription]);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.navigationController popViewControllerAnimated:YES];
}];
});
}
}); }
for videoURL in your didFinishPickingMediaWithInfo method call above function with below code :
self.library = [[ALAssetsLibrary alloc] init];
[self downloadVideo:[info valueForKey:UIImagePickerControllerMediaURL]];
I hope this works . . .
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([multipleCapture isOn])
{
images=info[UIImagePickerControllerOriginalImage];
currentDate=[NSDate date];
myString=[dateFormatter stringFromDate:currentDate];
customText=imagetext.text;
saveImage.dateTime=myString;
saveImage.imageName=info[UIImagePickerControllerOriginalImage];
saveImage.customText=customText;
[imageArray addObject:saveImage];
[imageArray sortUsingDescriptors:sortDescriptors];
if ([imageArray count] == 1)
{
UIGraphicsBeginImageContext(images.size);
myString=[[imageArray objectAtIndex:0]valueForKey:#"dateTime"];
[images drawInRect:CGRectMake(0, 0, images.size.width, images.size.height)];
NSDictionary* attributes = #{NSFontAttributeName : [UIFontboldSystemFontOfSize:75],NSStrokeColorAttributeName :[UIColor blackColor],NSForegroundColorAttributeName : [UIColor yellowColor],NSStrokeWidthAttributeName : #-2.0};
[myString drawAtPoint:CGPointMake(120,70)withAttributes:attributes];
[customText drawAtPoint:CGPointMake(870, 70)withAttributes:attributes];
newImage= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage,self,#selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
}
}
}
/* I worked on this code. The issue is when I try to make multiple shot it shows "Received Memory Warning" and my app got crashed.
In this I tried to count the images,at the same time It counts "Camera not ready mode" also So that my taken images saved multiple times.
How can I solve this issue ? */
Try this,
int counter;
NSMutableArray * imageArray;
-(void)takePicture
{
counter=0;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image= [info objectForKey:UIImagePickerControllerEditedImage];
[imageArray addObject:image];
counter++;
if (counter<PhotoCount)
{
[self dismissModalViewControllerAnimated:NO];
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:NO];
[imagePicker release];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
PhotoCount is the number of photos you want to take.
This code for capturing image one by one from camera,but after taking one image next time camera will open but with black screen(like it,s shutter close).all other ios version its working,but not working in ios 8.please tell me how can i solve it?
-(void)openCamera
{
if(![PickerHandler doesDeviceSupportMediaType:ITEM_TYPE_PHOTO])
{
[PickerHandler showNoDeviceSupportWarningForMediaType:ITEM_TYPE_PHOTO withDelegate:self];
}
else
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
[self presentViewController:picker animated:YES completion:nil];
}
}
Go to Settings > Privacy > Pictures ... and check if your app have permission.
In the code, use this to verify camera access.
- (BOOL)authorizedCameraAccess
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
return (status == AVAuthorizationStatusAuthorized);
}
this code is not work in simulator.
UIImagePickerController *videoScreen = [[UIImagePickerController alloc] init];
videoScreen.sourceType = UIImagePickerControllerSourceTypeCamera;
videoScreen.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
videoScreen.allowsEditing = NO;
videoScreen.delegate = self;
[self presentViewController:videoScreen animated: YES completion:NO];
Implement This method
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:NO completion:NO];
}
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.