UIImagePickerController让我拍摄照片和视频,但不会让我保存或“使用”它们

时间:2013-12-26 18:16:53

标签: ios iphone objective-c uiimage uiimagepickercontroller

我正在制作一个简单的照片和视频捕捉应用程序。该应用程序成功允许用户在按下按钮时拍摄照片或视频。但是,只要您完成拍摄照片或视频,它就会提供2个选项:“重拍”和“使用照片”或“使用视频”,具体取决于您使用的是哪一个。

如果用户点击“重拍”,那么只需让他们重新拍摄新照片或视频,但当用户点按“使用照片”或“使用视频”时,它就不会执行任何操作。应用程序只需将它们带回主应用程序屏幕。

我对如何访问“使用照片”/“使用视频”按钮感到困惑,这样我就可以编辑它的代码,并将图像和视频保存到相机胶卷。

这是我到目前为止的代码:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property UIImagePickerController * pickerController;

-(IBAction)showCameraUI;

@end

ViewController.m:

#import "ViewController.h"


@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

NSLog(@"%ld", (long)UIImagePickerControllerSourceTypeCamera); }

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)showCameraUI{

UIImagePickerController * pickerController = [[UIImagePickerController alloc]init];

pickerController.mediaTypes = [UIImagePickerController  availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;


[self presentViewController:pickerController animated:YES completion:NULL];

}

@end

3 个答案:

答案 0 :(得分:4)

您可以使用didFinishPickingMediaWithInfo的{​​{1}}代表保存图片。

为此你需要这样做:

UIImagePickerController

答案 1 :(得分:3)

请尝试使用此

- (void)captureVideoOrPhoto{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker setDelegate:self];

    if (self.isSourceTypeCamera) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }else{
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]&&self.isSourceTypeCamera){

        [imagePicker setShowsCameraControls:YES];
        [imagePicker setMediaTypes:[[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,(NSString*)kUTTypeImage, nil]];

        if (self.isVideoMode) {
            imagePicker.videoMaximumDuration = 300;
            [imagePicker setCameraCaptureMode:UIImagePickerControllerCameraCaptureModeVideo];
        }else{
            [imagePicker setCameraCaptureMode:UIImagePickerControllerCameraCaptureModePhoto];
        }
        [self presentViewController:imagePicker animated:YES completion:nil];
    }else{
        if (self.isVideoMode) {
            [imagePicker setMediaTypes:[[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil]];
        }
        [self presentViewController:imagePicker animated:YES completion:nil];
    }

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
        NSString *moviePath = [videoUrl path];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
        }

        NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *strFile = [Shared getAUniqueFileName];
        NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/%@.mp4",strFile];

    }else if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){
        NSLog(@"image captured");

        UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImageJPEGRepresentation(image, 0.0);
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *strFile = [Shared getAUniqueFileName];
        NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/%@.jpeg",strFile];
        [imageData writeToFile:tempPath atomically:NO];
    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

答案 2 :(得分:0)

使用AFNetworking 3.0上传文件(视频,图片)

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    [serializer setValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"];
    manager.requestSerializer = serializer;
    NSString *strUrl = @"pass url";

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TestVideo" ofType:@"mp4"];
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];

    NSDictionary *param = @{@"method":@"uploadMedia",@"media_comment":@"hi",@"fileToUpload":fileData};

    [manager POST:strUrl parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"preogress %f",uploadProgress.fractionCompleted);

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"uploaded successfully");

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"fail to upload");
    }];