我想上传一个 PHAsset 到服务器。现在我的 PHAsset 可以是照片或视频。对于本地设备上存在的 PHAssets,我能够成功地做到这一点。但是,如果 PHAsset 不在本地设备上而是存在于 iCloud 上怎么办。
是直接从iCloud上传到服务器还是先下载到本地设备再上传到服务器操作
此外,如果有人能告诉我如何做到这一点,我将不胜感激。
更新:当我们允许网络访问从 iCloud 下载图像时,出现以下错误。
{
PHImageErrorKey = "Error Domain=CloudPhotoLibraryErrorDomain Code=1000 \"Error fetching asset with identifier: AUXxudUWWzHheX1dXyRDgXpZN5ty\" UserInfo={NSLocalizedDescription=Error fetching asset with identifier: AUXxudUWWzHheX1dXyRDgXpZN5ty, NSUnderlyingError=0x7fb00a5e9480 {Error Domain=CKErrorDomain Code=6 \"Fetching asset failed\" UserInfo={NSLocalizedDescription=Fetching asset failed, NSUnderlyingError=0x7fb00a511600 {Error Domain=CKInternalErrorDomain Code=2022 \"Fetching asset failed\" UserInfo={NSLocalizedDescription=Fetching asset failed, NSUnderlyingError=0x7fb00a5b6fc0 {Error Domain=com.apple.mmcs Code=16 \"(null)\"}}}}}}";
PHImageResultDeliveredImageFormatKey = 0;
PHImageResultIsInCloudKey = 1;
PHImageResultWantedImageFormatKey = 20002;
}
最佳答案
您应该在您的移动应用程序中下载 Assets ,然后上传到您的服务器。由于沙盒和其他隐私和安全规则,我认为无法直接从 iCloud 上传。
如果您使用PHCachingImageManager
和requestImageForAsset
,您应该为PHImageRequestOptions
指定特殊选项networkAccessAllowed
允许iCloud按照 documentation 中的说明获取数据:
If YES, and the requested resource data is not stored on the local device, Photos downloads that data from iCloud.
NOTE: You should enable iCloud entitlement with iCloud Documents service and setup default container. See more details here Enabling CloudKit in Your App. As I can see, it's required when you use
networkAccessAllowed = true
例如,您可以像这样为 Assets 管理器设置选项:
PHImageRequestOptions *requestOptions = [PHImageRequestOptions new];
requestOptions.synchronous = NO;
requestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.networkAccessAllowed = YES;
然后请求数据
[self.cachingImageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:requestOptions resultHandler:^(UIImage * _Nullable resultImage, NSDictionary * _Nullable info) {
...
}];
在这种情况下,您将看到 iCloud 如何在调用 resultHandler
之前在文档选择器中获取数据,您可以在其中处理请求的 Assets 。
关于ios - 从 iCloud 上传 PHAsset(即本地设备上不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749624/