iOS开发 开发抖音分享图片功能

2021-09-10  本文已影响0人  深圳阳光

首先,我这边做的是将界面截图保存到相册,获取到相册里面最后一张截图的图片再分享到抖音.以下是分享到抖音的调用代码:

    [YSBCommonTool getLastImageCallImageBack:^(PHAsset *asset) {
       
        NSMutableArray<NSString *> *mediaLocalIdentifiers = [NSMutableArray array];
        [mediaLocalIdentifiers addObject:asset.localIdentifier];
        if (mediaLocalIdentifiers.count > 0) {
            DouyinOpenSDKShareRequest *req = [[DouyinOpenSDKShareRequest alloc] init];
            req.mediaType = DouyinOpenSDKShareMediaTypeImage;
            req.shareAction = DouyinOpenSDKShareTypePublishMedia;
            
            req.localIdentifiers = [mediaLocalIdentifiers copy];
            req.landedPageType = DouyinOpenSDKLandedPageClip;
            [req sendShareRequestWithCompleteBlock:^(DouyinOpenSDKShareResponse * _Nonnull Response) {
                if (Response.isSucceed) {
                    NSLog(@"分享成功");
                    
                }else{
                    NSLog(@"分享失败");
                    
                }
            }];
        }else{
            NSLog(@"没有获取到图片");
        }
    }];

以下是封装的获取相册最后一张图片

+ (void)getLastImageCallImageBack:(void (^)(PHAsset *asset))imageBlock {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
        NSLog(@"相册权限未开放");
        [YSBCommonTool showAlertViewControllerWithTitle:@"无法访问相册" andMessage:@"请在iPhone的""设置-隐私-相册""中允许访问相册" andLeftBtnTitle:@"取消" andRightBtnTitle:@"设置" andLeftBtnAction:^(UIAlertAction * _Nonnull action) {
            
        } andRightBtnAction:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
        }];
        return;
    }
    // 获取最近的照片
    PHAsset *asset = [YSBCommonTool latestAsset];
    if (!asset) {
        NSLog(@"相册里没有照片");
        YSBToastInCenter(@"相册里没有照片");
        return;
    }

    imageBlock(asset);
}

+ (PHAsset *)latestAsset {
    // 获取所有资源的集合,并按资源的创建时间排序
    PHFetchOptions *options = [[PHFetchOptions alloc] init];
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
    return [assetsFetchResults firstObject];
}
上一篇下一篇

猜你喜欢

热点阅读