美文网首页
iOS开发 开发抖音分享图片功能

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

作者: 深圳阳光 | 来源:发表于2021-09-10 09:54 被阅读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];
    }
    

    相关文章

      网友评论

          本文标题:iOS开发 开发抖音分享图片功能

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