Pick Image From Gallery or Camera & Post
In Appdelegate :
1 ) Import In viewcontroller.h :-
Framework :- MediaPlayer
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAsset.h>
Set Delegate :- < UIImagePickerControllerDelegate >
@property (strong,nonatomic) MPMoviePlayerController *player;
2 ) Use In viewcontroller.m :-
- (IBAction)addmedia:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Add Poster?" delegate:self cancelButtonTitle:@"Photo Library" otherButtonTitles:@"Camera", nil];
[alert setTag:1];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary | UIImagePickerControllerSourceTypeCamera];
[imagePicker setMediaTypes:mediaTypesAllowed];
if(buttonIndex == [alertView cancelButtonIndex])
{
imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
}
else
{
if(buttonIndex == 1)
{
imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
}
}
[self presentViewController:imagePicker
animated:YES completion:nil];
}
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
_mediaView.frame = CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height) ;
[self.view addSubview:_mediaView];
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSURL *url = info[UIImagePickerControllerMediaURL];
mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImageWriteToSavedPhotosAlbum(image, self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([NSString stringWithFormat:@"%@",url]))
{
UISaveVideoAtPathToSavedPhotosAlbum([NSString stringWithFormat:@"%@",url],
self,
@selector(video:finishedSavingWithError:contextInfo:),
nil);
}
}
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
//check the media type string so we can determine if its a video
if ([mediaType isEqualToString:@"public.movie"]){
NSLog(@"info:%@",info);
videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
// NSData *webData = [NSData dataWithContentsOfURL:videoURL];
_player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[_player.view setFrame:self.mediaImage.frame];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSTimeInterval durationInSeconds = 0.0;
if (asset)
durationInSeconds = CMTimeGetSeconds(asset.duration);
NSLog(@"duration:%f",durationInSeconds);
if(durationInSeconds > 6.0){
[[[UIAlertView alloc] initWithTitle:nil
message:@"You can only upload 6sec Video."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil] show];
}
else
{
[_player.view setFrame:_mediaView.frame];
[self.mediaView addSubview:_player.view];
[_player play];
}
}
if([mediaType isEqualToString:@"public.image"])
{
[_player.view removeFromSuperview];
self.mediaImage.image=[info objectForKey:UIImagePickerControllerOriginalImage];
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)
error contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Save failed"
message: @"Failed to save image/video"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)video:(UIImage *)image
finishedSavingWithError:(NSError *)
error contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Save failed"
message: @"Failed to save image/video"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
//------------------------------------------------------------
//-------Post Image or Video Using ASIFormDataRequest
if ([mediaType isEqualToString:@"public.movie"])
{
NSData *uploadData = [NSData dataWithContentsOfURL:videoURL];
[request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
[request setData:uploadData withFileName:@"hangover.mp4" andContentType:@"multipart/form-data" forKey:@"video"];
}
else if([mediaType isEqualToString:@"public.image"])
{
NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((_mediaImage.image), 0.5)];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
[request setData:imgData withFileName:@"abc.jpg" andContentType:@"multipart/form-data" forKey:@"image"];
}
No comments:
Post a Comment