美文网首页
相册浅谈

相册浅谈

作者: Carden | 来源:发表于2017-05-05 15:09 被阅读30次

    CocoaPods导入三方库

    platform:ios,'8.0'
    
    target ‘你的应用’ do
    
    pod 'ReactiveCocoa','~>2.5'
    
    pod 'Masonry'
    
    end
    
    

    导入RecordUI---Demo下载

    视频录制库

    .pch文件导入#import "JHRecord.h"

    看一下.h头文件详情

    控制器加载录制View和上传View

    @property (strong, nonatomic) JHRecordView                *recordView;
    @property (strong, nonatomic) JHUploadView                *uploadView;
    
    __strong typeof(self) weakSelf = self;//避循环引用
    [self.view addSubview:weakSelf.uploadView];
    [self.view addSubview:weakSelf.recordView];
    
    /**
     *  录制View
     */
    -(JHRecordView*)recordView{
        if (!_recordView) {
            _recordView = [[JHRecordView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT-20)];
            [_recordView.recordViewSubject subscribeNext:^(NSDictionary *dict) {
                NSString *btnType = [dict objectForKey:@"btnType"];
                if ([btnType isEqualToString:@"相册"])     {
                    self.picker.mediaTypes = @[@"public.movie"];//@[(NSString *)kUTTypeMovie];// 仅视频
                    self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    [self presentViewController:self.picker animated:YES completion:nil];
                }
                else if ([btnType isEqualToString:@"上传"]){
                    [ViewUtil rollAnimation:self.view];
                    
                    self.uploadView.coverImage = self.recordView.recordEngine.originVideoCover;
                    
                    [self.view bringSubviewToFront:self.uploadView];
                }
            }];
        }
        return _recordView;
    }
    
    /**
     *  上传View
     */
    -(JHUploadView*)uploadView{
        if (!_uploadView) {
            _uploadView = [[JHUploadView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT-20)];
            [_uploadView.uploadViewSubject subscribeNext:^(NSDictionary *dict) {
                NSString *btnType = [dict objectForKey:@"btnType"];
                if ([btnType isEqualToString:@"相册"])     {
                    self.picker.mediaTypes = @[@"public.image"];// 仅图片
                    self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    NSArray *types = [UIImagePickerController availableMediaTypesForSourceType:self.picker.sourceType];
                    NSLog(@"所有的文件类型:%@",types);
                    [self presentViewController:self.picker animated:YES completion:nil];
                }
                else if ([btnType isEqualToString:@"上传"]){
                    //[TipUtil tipSuccess:@"主人,往哪里传呀?"   DismissAfter:0.6];
                }
                else if ([btnType isEqualToString:@"取消"]){
                    [ViewUtil rollAnimation:self.view];
                    
                    [self.view bringSubviewToFront:self.recordView];
                }
            }];
        }
        return _uploadView;
    }
    
    

    录制选视频,上传选封面,两个功能在控制器合二为一

    1、导入相册系统库

    #import <MobileCoreServices/MobileCoreServices.h>
    

    2、声明协议

    <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    

    3、懒加载UIImagePickerController

    // 相册控制器
    @property (strong, nonatomic) UIImagePickerController     *picker;
    /**
     *  本地相册视频库
     */
    - (UIImagePickerController *)picker {
        if (_picker == nil) {
            _picker = [[UIImagePickerController alloc] init];
            _picker.delegate = self;
            _picker.allowsEditing = NO;
            
            
            
            [[self rac_signalForSelector:@selector(imagePickerControllerDidCancel:)] subscribeNext:^(id x) {
                [self.picker dismissViewControllerAnimated:YES completion:nil];
            }];
            
            [[self rac_signalForSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)] subscribeNext:^(RACTuple *tuple) {
                NSDictionary *infoDict = tuple.second;
                NSLog(@"选择的结果:%@",infoDict);
                NSString *mediaType = [infoDict objectForKey: UIImagePickerControllerMediaType];
                if ([mediaType isEqualToString:@"public.movie"]) {
                    // 视频
                    NSURL *videoUrl=(NSURL*)[infoDict objectForKey:UIImagePickerControllerMediaURL];
                    NSString *moviePath = [videoUrl path];
                    [[NSFileManager defaultManager] copyItemAtPath:moviePath toPath:[self.recordView.recordEngine uploadPath] error:nil];
                    [self.recordView.recordEngine movieToImageHandler:^(UIImage *movieImage) {
                        NSLog(@"封面:\n%@",movieImage);
                    }];
                    [self.recordView startVideoPlayer:[NSURL fileURLWithPath:self.recordView.recordEngine.videoPath]];
                }
                else if ([mediaType isEqualToString:@"public.image"]) {
                    // 封面
                    UIImage *selectedImage = [infoDict objectForKey:UIImagePickerControllerOriginalImage];
                    if (selectedImage) self.uploadView.coverImage = selectedImage;
                }
                [_picker dismissViewControllerAnimated:YES completion:nil];
            }];
        }
        return _picker;
    }
    

    4、中文模式

    中文简体

    5、导入CocoaPods如果报错,Build Settings搜索weak设置Weak References·····为YES


    允许引用计数

    相关文章

      网友评论

          本文标题:相册浅谈

          本文链接:https://www.haomeiwen.com/subject/iwxstxtx.html