尝试将图像保存到照片库时,iOS 10应用程序崩溃

时间:2022-03-09 23:10:09

I'm trying to save an image to the photo library in Swift 3 (I'm working with Xcode 8).

我正在尝试将图像保存到Swift 3中的照片库(我正在使用Xcode 8)。

ViewController Code:

ViewController代码:

func shareImage(image: UIImage) {
    let items = [image]

    var activityVC: UIActivityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
    let excludeActivities: [UIActivityType] = [UIActivityType.airDrop,
                                               UIActivityType.assignToContact,
                                               UIActivityType.addToReadingList,
                                               UIActivityType.copyToPasteboard]

    activityVC.excludedActivityTypes = excludeActivities

    self.present(activityVC, animated: true, completion: nil)
}

When I run the application, and click on the button to take the screenshot (converting it to image, ..., that's all working perfectly), the app asks for permission to access the photo library, I tap the "OK" button, and then the app crashes. The image is not saved in the photo library.

当我运行应用程序,然后单击按钮获取屏幕截图(将其转换为图像,...,这一切都完美无缺)时,应用程序要求访问照片库的权限,我点击“确定”按钮,然后应用程序崩溃。图像未保存在照片库中。

The only clue I get from Xcode is the following:

我从Xcode获得的唯一线索如下:

2016-09-28 11:24:27.216043 Ajax Kids[4143:1545362] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Media/PhotoData/Photos.sqlite?readonly_shm=1 options:{
NSPersistentStoreFileProtectionKey = NSFileProtectionCompleteUntilFirstUserAuthentication;
NSReadOnlyPersistentStoreOption = 1;
NSSQLitePersistWALOption = 1;
NSSQLitePragmasOption =     {
    "journal_mode" = WAL;
};
} ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={reason=Failed to access file: 1} with userInfo dictionary {
reason = "Failed to access file: 1";
}
2016-09-28 11:24:27.216433 Ajax Kids[4143:1545362] [Migration] Unexpected error opening persistent store <private>, cannot attempt migration <private>)
2016-09-28 11:24:27.216568 Ajax Kids[4143:1545362] [Migration] Failed to open store <private>.  Requires update via assetsd (256: <private>)

Does anyone have any idea how to fix this?

有谁知道如何解决这个问题?

Thanks in advance!

提前致谢!

UPDATE

UPDATE

Sharing the image on Social Media works fine, so the problem is specified to saving the image in the photo library.

在社交媒体上共享图像可以正常工作,因此指定了将图像保存在照片库中的问题。

4 个解决方案

#1


30  

Add new records in your new InfoPlist.strings file.

在新的InfoPlist.strings文件中添加新记录。

<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>

UPD: iOS 11 key

UPD:iOS 11密钥

#2


9  

On iOS 11, there is a new property called NSPhotoLibraryAddUsageDescription, similar to NSPhotoLibraryUsageDescription. See https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

在iOS 11上,有一个名为NSPhotoLibraryAddUsageDescription的新属性,类似于NSPhotoLibraryUsageDescription。请参阅https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

#3


1  

Try to force request permissions like this:

尝试强制这样的请求权限:

PHPhotoLibrary.requestAuthorization { status in
  if status == .authorized {
    //do things
  }
}

do not forget import Photos. Hope this helps.

别忘了导入照片。希望这可以帮助。

#4


0  

I found the culprit in my particular case. We are using Leanplum for analytics and push notifications. The Leanplum.syncResourcesAsync method was causing the opening of the photo library to crash. It took a couple of days to find as I wasn't aware Leanplum was doing anything to hook into a user's photo library... which in itself is concerning.

我在特定情况下找到了罪魁祸首。我们正在使用Leanplum进行分析和推送通知。 Leanplum.syncResourcesAsync方法导致照片库的打开崩溃。花了几天时间才发现,因为我不知道Leanplum正在做任何事情来挂钩用户的照片库......这本身就是关注的。

We weren't using the functionality this particular method brings, so were able to just remove the method call and the photo library stopped crashing.

我们没有使用此特定方法带来的功能,因此只能删除方法调用并且照片库停止崩溃。

#1


30  

Add new records in your new InfoPlist.strings file.

在新的InfoPlist.strings文件中添加新记录。

<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>

UPD: iOS 11 key

UPD:iOS 11密钥

#2


9  

On iOS 11, there is a new property called NSPhotoLibraryAddUsageDescription, similar to NSPhotoLibraryUsageDescription. See https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

在iOS 11上,有一个名为NSPhotoLibraryAddUsageDescription的新属性,类似于NSPhotoLibraryUsageDescription。请参阅https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

#3


1  

Try to force request permissions like this:

尝试强制这样的请求权限:

PHPhotoLibrary.requestAuthorization { status in
  if status == .authorized {
    //do things
  }
}

do not forget import Photos. Hope this helps.

别忘了导入照片。希望这可以帮助。

#4


0  

I found the culprit in my particular case. We are using Leanplum for analytics and push notifications. The Leanplum.syncResourcesAsync method was causing the opening of the photo library to crash. It took a couple of days to find as I wasn't aware Leanplum was doing anything to hook into a user's photo library... which in itself is concerning.

我在特定情况下找到了罪魁祸首。我们正在使用Leanplum进行分析和推送通知。 Leanplum.syncResourcesAsync方法导致照片库的打开崩溃。花了几天时间才发现,因为我不知道Leanplum正在做任何事情来挂钩用户的照片库......这本身就是关注的。

We weren't using the functionality this particular method brings, so were able to just remove the method call and the photo library stopped crashing.

我们没有使用此特定方法带来的功能,因此只能删除方法调用并且照片库停止崩溃。