iOS开发资料收集区

MWPhotoBrowser播放iCloud云上视频

2019-03-01  本文已影响0人  Maj_sunshine

距离上一个文章又是两个月...

起因


翻车样子


ezgif.com-video-to-gif.gif

开始解决

从iCloud上同步
+ (void)getVideoWithAsset:(PHAsset *)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler success:(void (^)(NSURL *video, BOOL isInCloud))success failure:(void (^)(NSString *errorMessage))failure {
    PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
    options.version = PHVideoRequestOptionsVersionOriginal;
    options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
    options.networkAccessAllowed = YES;
    options.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
        WYDo_Block_Safe_Main4(progressHandler, progress, error, stop, info)
    };
    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset* avasset, AVAudioMix* audioMix, NSDictionary* info){
        BOOL cloud = [[info objectForKey: PHImageResultIsInCloudKey] boolValue];
        if (avasset) {
            AVURLAsset *videoAsset = (AVURLAsset*)avasset;
            WYDo_Block_Safe_Main2(success, videoAsset.URL, cloud);
        } else {
            WYDo_Block_Safe_Main1(failure, @"下载失败");
        }
        
    }];
}

这个代码很死板的,目的是拿到最终转化后的视频URL.
可以看下获取前和获取后的URL.

开始处理
- (PHAsset *)wy_asset {
    return objc_getAssociatedObject(self, @selector(wy_asset));
}
- (void)setWy_asset:(PHAsset *)wy_asset {
    objc_setAssociatedObject(self, @selector(wy_asset), wy_asset, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

2 在MWPhotoBrowser的代理中,获取asset并赋值

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index {
    MWPhoto *photo = [_photos objectAtIndex:index];
    PHAsset *asset = object_getIvar(photo, class_getInstanceVariable([MWPhoto class], "_asset"));
    photoBrowser.wy_asset = asset;
}

因为在MWPhoto中asset是私有属性,这里用runtime获取了下

+ (void)load {
    WY_ExchangeInstanceImp(@selector(WY_playVideo:atPhotoIndex:), NSSelectorFromString(@"_playVideo:atPhotoIndex:"));
}

其中, WY_ExchangeInstanceImp

#define WY_ExchangeInstanceImp(sel1,sel2) \
method_exchangeImplementations(class_getInstanceMethod(self, sel1), class_getInstanceMethod(self, sel2));

2 在- (void)WY_playVideo:(NSURL *)videoURL atPhotoIndex:(NSUInteger)index中获取iCloud视频地址

- (void)WY_playVideo:(NSURL *)videoURL atPhotoIndex:(NSUInteger)index {
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("photo", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_enter(group);
    __block NSURL *videoUrl = nil;
    dispatch_async(queue, ^{
        [MWPhotoBrowser getVideoWithAsset:self.wy_asset progressHandler:nil success:^(NSURL *video, BOOL isInCloud) {
            videoUrl = video;
            dispatch_group_leave(group);
        } failure:^(NSString *errorMessage) {
            dispatch_group_leave(group);
        }];
    });
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self WY_playVideo:videoUrl atPhotoIndex:index];
        });
    });
}

这里是异步的获取URL,所以用了任务组dispatch_group_t

到这里end

上一篇下一篇

猜你喜欢

热点阅读