多媒体之摄像头、相册

简介:

网络这块要学的东西还有好多,比如套接字等,目前只是了解了AFNetworking。掌握了它对网络这块基本功能都可以实现,问题不大,等有机会再深入学习网络这块。目前先把IOS的整体模块都了解学习遍,针对特别的一些模块随后仔细深入理解。现在来看下多媒体这块。


//
//  ViewController.m
//  Photo
//
//  Created by City--Online on 15/5/4.
//  Copyright (c) 2015年 CYW. All rights reserved.
//
 
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
 
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
@property(nonatomic,strong)UIImageView *ImgView;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的相册";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"拍照" style:UIBarButtonItemStyleDone target:self action:@selector(pickerImg)];
    self.navigationController.navigationBar.translucent=NO;
    self.view.backgroundColor=[UIColor redColor];
     
    self.ImgView=[[UIImageView alloc]initWithFrame:self.view.frame];
    self.ImgView.backgroundColor=[UIColor blueColor];
    [self.view addSubview:self.ImgView];
    [self availableMediaTypes];
    [self availableCaptureModes];
}
 
-(void)pickerImg
{
    UIImagePickerController *ImgPick=[[UIImagePickerController alloc]init];
    //    typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
    //        UIImagePickerControllerSourceTypePhotoLibrary, 图片列表
    //        UIImagePickerControllerSourceTypeCamera,       摄像头
    //        UIImagePickerControllerSourceTypeSavedPhotosAlbum 相机相册
    //    };
    
    ImgPick.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    ImgPick.delegate=self;
    //设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁。只有当showsCameraControls属性为true时才有效果
    ImgPick.editing=YES;
    //设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
//    ImgPick.showsCameraControls  = YES;
    //设置使用后置摄像头,可以使用前置摄像头
//    ImgPick.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    //设置闪光灯模式
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
     UIImagePickerControllerCameraFlashModeOff  = -1,
     UIImagePickerControllerCameraFlashModeAuto = 0,
     UIImagePickerControllerCameraFlashModeOn   = 1
     };
     */
//    ImgPick.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
     
    //设置相机支持的类型,拍照和录像
    ImgPick.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];
    //叠加View
//    ImgPick.cameraOverlayView=self.view;
    //旋转
//    ImgPick.cameraViewTransform = CGAffineTransformMakeScale(0.5, 0.5);
    //最长时间支持
//    ImgPick.videoMaximumDuration=10;
//    枚举 视频质量
//    ImgPick.videoQuality=UIImagePickerControllerQualityTypeHigh;
     
//    // 可以通过判断cameraCaptureMode来进行拍照或录像 前提是sourceType=UIImagePickerControllerSourceTypeCamera 上面注释的属性也是
//    if (ImgPick.cameraCaptureMode==UIImagePickerControllerCameraCaptureModePhoto) {
//        [ImgPick takePicture];
//    }
//    else
//    {
//        [ImgPick startVideoCapture];
//        sleep(10);
//        [ImgPick stopVideoCapture];
//    }
     
    [self presentViewController:ImgPick animated:YES completion:nil];
}
 
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 
{
     
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
         
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
         
        //得到选择后沙盒中图片的完整路径
       NSString *filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
        NSLog(@"%@",filePath);
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:nil];
         
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
                                    CGRectMake(50, 120, 100, 100)] ;
         
        smallimage.image = image;
        //加在视图中
        [self.view addSubview:smallimage];
         
    }
     
}
 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissViewControllerAnimated:YES completion:nil];
}
 
//以下这些方法以后可以用到
 
// 相册是否可用
- (BOOL) isPhotoLibraryAvailable{
    return [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary];
}
//是否支持摄像头
-(BOOL)isCameraAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
//是否支持前后摄像头
-(BOOL)isFontCameraAvailable
{
    //    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
    //        UIImagePickerControllerCameraDeviceRear,  后置摄像头
    //        UIImagePickerControllerCameraDeviceFront  前置摄像头
    //    };
     
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
//是否支持闪光灯、手电筒
-(BOOL)isFlashAvailable
{
    return [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront];
}
//支持的媒体类型
-(void)availableMediaTypes
{
    NSArray *array=[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    for (NSString* mediaType in array) {
//        (NSString *)kUTTypeImage]) 在MobileCoreServices框架,需要引入并添加#import <MobileCoreServices/MobileCoreServices.h>
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
            //支持摄像
//            public.image
            NSLog(@"%@",mediaType);
            NSLog(@"支持摄像");
            break;
        }
    }
    NSLog(@"%ld",array.count);
    for (int i=0; i<array.count; i++) {
        NSLog(@"%@",[array objectAtIndex:i]);
    }
}
-(void)availableCaptureModes
{
    //typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
    //    UIImagePickerControllerCameraCaptureModePhoto,
    //    UIImagePickerControllerCameraCaptureModeVideo
    //};
    //支持的相机模式 拍照还是摄像
    NSArray *array=[UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceFront];
    NSLog(@"%ld",array.count);
    for (int i=0; i<array.count; i++) {
        NSLog(@"%@",[array objectAtIndex:i]);
    }
     
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

 


相关文章
|
7月前
|
小程序 JavaScript 前端开发
微信小程序的音频视屏播放
微信小程序的音频视屏播放
128 0
|
7月前
uniapp制作录音播放功能
uniapp制作录音播放功能
244 0
【3D相册】零基础完成3D相册并配上背景音乐
简介:【3D相册】零基础完成3D相册并配上背景音乐
【3D相册】零基础完成3D相册并配上背景音乐
|
存储 编解码 缓存
带你走进多媒体世界:视频文件是怎么播放出来的
本文将聚焦在说明多媒体世界中的视频文件是怎么播放出来的。一个多媒体文件播放涉及到哪些过程?人体通过肉眼看到画面、通过耳朵听到视频声音,这中间经过了哪些处理?本文将尽可能通过简单易懂的文字讲述一个视频文件的播放流程。
带你走进多媒体世界:视频文件是怎么播放出来的
短视频APP开发,调用系统相册,选择音频、视频、图片
短视频APP开发,调用系统相册,选择音频、视频、图片
308 0
|
开发工具
推荐15个在线多媒体(图片、音频、视频)编辑器,互联网营销
  在处理多媒体文件的时候,我们通常都是使用自己电脑上已安装好的桌面应用程序,如果某天我们在使用的公共电脑上没有我们需要的软件,更坏的情况是,这些公共电脑不允许安装软件,这时候就需要在线的编辑器了。最近几年,Web应用程序越来越受到关注,原因在于它的简单、方便和轻巧,只要你有一个连接到互联网的浏览器,就可以马上处理你的图片、音频和视频。
2754 0