网络这块要学的东西还有好多,比如套接字等,目前只是了解了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