iOS10 UIImageWriteToSavedPhotosAlbum TCC__CRASHING_DUE_TO_PRIVACY_VIOLATION

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS10 UIImageWriteToSavedPhotosAlbum TCC__CRASHING_DUE_TO_PRIVACY_VIOLATION相关的知识,希望对你有一定的参考价值。

我想在ios 10中创建并保存UIImage。我可以通过使用UIView快照或使用UIGraphicsImageRenderer(如下所示)来创建它。

- (UIView *)createIcon
{
    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(200, 200)];

    UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) {
        [[UIColor darkGrayColor] setStroke];
        [context strokeRect:renderer.format.bounds];
        [[UIColor colorWithRed:158/255.0 green:215/255.0 blue:245/255.0 alpha:0.5] setFill];
        [context fillRect:CGRectMake(30, 30, 140, 140)];
        [[UIColor colorWithRed:243/255.0 green:122/255.0 blue:216/255.0 alpha:0.3] setFill];
        CGContextFillEllipseInRect(context.CGContext, CGRectMake(30, 30, 120, 120));
    }];

    UIImageView *showImage = [[UIImageView alloc] initWithImage:image];

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    return showImage;
}

但是,当我尝试通过包含第二个最后一个语句来使用UIImageWriteToSavedPhotosAlbum进行保存时,Xcode将使用以下错误报告进入Debug

(lldb) bt
* thread #5, queue = 'com.apple.root.default-qos', stop reason = signal SIGABRT
  * frame #0: 0x00000001101611a6 libsystem_kernel.dylib`__abort_with_payload + 10
    frame #1: 0x000000011015b86e libsystem_kernel.dylib`abort_with_payload_wrapper_internal + 89
    frame #2: 0x000000011015b89b libsystem_kernel.dylib`abort_with_payload + 9
    frame #3: 0x0000000108ab3af7 TCC`__CRASHING_DUE_TO_PRIVACY_VIOLATION__ + 182
    frame #4: 0x0000000108ab3a41 TCC`__TCCAccessRequest_block_invoke.77 + 665
    frame #5: 0x0000000108ab7273 TCC`__tccd_send_block_invoke + 274

因此即使我没有使用相机来创建图像,将其保存到相册也是对隐私的侵犯(嘘声,嘘声)!我知道有可能设置privacy keys in the plist但是有更明智的选择吗?

澄清

我希望在应用程序中使用渲染图像(或查看快照)作为资产。那么有没有更明智的选择呢?

也许我应该问

是否有另一个保存图像的地方(即PNG或JPG文件),所以隐私不是问题?

答案

您可以将任何UIImage保存到相册中,但首先您必须要求用户允许这样做,因为它确实是一个隐私问题。如果他们没有给您访问权限,则根本无法保存图像。

最明智的方法是将所需的隐私密钥添加到info.plist中。

这是info.plist xml定义,尽管在属性列表中添加键更容易:

<key>NSPhotoLibraryAddUsageDescription</key>
<string>Message requesting the ability to add to the photo library</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Message requestion the ability to access the photo library</string>

如果您添加这些内容,那么当您第一次尝试访问或添加到照片库时,将显示一条弹出窗口,其中包含您的消息,允许用户决定他们是否希望您的应用具有访问权限。

将它放在info.plist文件中的一个很好的理由是,所有访问请求都在一个容易看见的地方而不是项目中的随机位置。

编辑

以下是如何将图像保存在不会引起问题的文档和隐私问题中:

NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[imageData writeToFile:filePath atomically:YES]; //Write the file

如果你想要一个jpg而不是一个png使用它:

NSData *imageData = UIImageJPEGRepresentation(image, 0.9); // Use whatever compression ratio you want instead of 0.9
另一答案

请遵循以下代码:

#import <Photos/Photos.h>

pragma mark - Save Image in photo album

- (void)addImageToCameraRoll:(UIImage *)image {
NSString *albumName = @"Your app name";

void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) {
    [[phphotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
        [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];

    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error creating asset: %@", error);
        }
    }];
};

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", albumName];
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
if (fetchResult.count > 0) {
    saveBlock(fetchResult.firstObject);
} else {
    __block PHObjectPlaceholder *albumPlaceholder;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
        albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;

    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
            if (fetchResult.count > 0) {
                saveBlock(fetchResult.firstObject);
            }
        } else {
            NSLog(@"Error creating album: %@", error);
        }
    }];
}
}

以上是关于iOS10 UIImageWriteToSavedPhotosAlbum TCC__CRASHING_DUE_TO_PRIVACY_VIOLATION的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发 如何适配iOS10

iOS - 如何适配iOS10(插曲)

iOS 8、iOS 9、iOS 10 和 iOS 11 上的 UITabBar 的高度是多少?

iOS:Xcode8以下真机测试iOS10.0和iOS10.1配置包

iOS10适配相关

iOS 9 和 iOS 10 CoreData 同时