我知道 Swift 和 Xcode 6 都仍处于测试阶段,但我认为在我的案例中存在一个与 Swift 或 Xcode 6 无关的结构性错误。如果 stackoverflow 社区认为这是一个不恰当的问题,我可以立即将其删除。
但让我们现在开始我的问题。我有一个 UIViewController,我正在尝试将相机胶卷中的最后一张图像添加到此 View Controller (显然是通过 UIImageView)。这是我的 viewDidLoad 方法:
override func viewDidLoad() {
super.viewDidLoad()
var assetLib = ALAssetsLibrary()
var url: NSURL = NSURL()
var imageView = UIImageView(frame: self.view.bounds)
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup!, stop: CMutablePointer<ObjCBool>) in
group.setAssetsFilter(ALAssetsFilter.allPhotos())
group.enumerateAssetsAtIndexes(NSIndexSet(index: group.numberOfAssets()-1), options: nil, usingBlock: {
(result: ALAsset!, index: Int, stop: CMutablePointer<ObjCBool>) in
if result {
var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation()
url = alAssetRapresentation.url()
if group == nil {
assetLib.assetForURL(url, resultBlock: {
(asset: ALAsset!) in
var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
var iref = assetRep.fullResolutionImage().takeUnretainedValue()
var image = UIImage(CGImage: iref)
imageView.image = image
self.view.addSubview(imageView)
}, failureBlock: {
(error: NSError!) in
NSLog("Error!", nil)
})
}
}
})
}, failureBlock: {
(error: NSError!) in
NSLog("Error!", nil)
})
}
问题是每次我编译程序时都会崩溃,Xcode 会提示我进入这个漂亮的小文件:
IdealityS`Swift._getOptionalValue <A>(Swift.Optional<A>) -> A:
0x6540: pushl %ebp
0x6541: movl %esp, %ebp
0x6543: pushl %ebx
0x6544: pushl %edi
0x6545: pushl %esi
0x6546: subl $0x8c, %esp
0x654c: calll 0x6551 ; Swift._getOptionalValue <A> (Swift.Optional<A>) -> A + 17
...
...
文件很长,所以我不会在这里发布全部内容...如您所见,它似乎与 Swift 可选值有关。这就是为什么我试图添加一个!在每个闭包变量之后,例如在 enumerateAssetsAtIndexes 方法 block 中
{(result: ALAsset!, index: Int!, stop: CMutablePointer<ObjCBool>!) in
...
}
一些元素有!之前,因为在类引用和一些互联网示例中我找到了它们(在本例中是结果)。好吧,在这个移动之后程序仍然崩溃,但是由于另一个原因......在
group.enumerateAssetsAtIndexes(NSIndexSet(index: group.numberOfAssets()-1), options: nil, usingBlock: {
行,我得到 EXT_BAD_ACCESS。
我对 Assets 之类的东西真的很陌生,我不知道为什么会这样。我在这里遵循了 Apple AV Foundation 编程指南中的示例:https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/01_UsingAssets.html我不知道我的改变是否与!可以帮助您了解发生了什么,但我发布它是因为我认为这是一个可能有帮助的有趣事实。 谢谢指教!
PS:如果您对代码有任何帮助,我们将不胜感激!
编辑:
这是回溯:
Attempt to add read-only file at path file:///var/mobile/Media/PhotoData/Photos.sqlite?readonly_shm=1 read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption. fatal error: Can't unwrap Optional.None
编辑: 在 Bill 的大力帮助下,我修改了代码,使应用程序不再崩溃!这是新版本:
override func viewDidLoad() {
super.viewDidLoad()
var assetLib = ALAssetsLibrary()
var url: NSURL = NSURL()
var imageView = UIImageView(frame: self.view.bounds)
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup?, stop: CMutablePointer<ObjCBool>) in
if group != nil {
group!.setAssetsFilter(ALAssetsFilter.allPhotos())
group!.enumerateAssetsAtIndexes(NSIndexSet(index: group!.numberOfAssets()-1), options: nil, usingBlock: {
(result: ALAsset!, index: Int, stop: CMutablePointer<ObjCBool>) in
if result {
var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation()
url = alAssetRapresentation.url()
}
})
}
else if group == nil {
assetLib.assetForURL(url, resultBlock: {
(asset: ALAsset!) in
if asset != nil {
var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
var iref = assetRep.fullResolutionImage().takeUnretainedValue()
var image = UIImage(CGImage: iref)
imageView.image = image
self.view.addSubview(imageView)
}
}, failureBlock: {
(error: NSError!) in
NSLog("Error!", nil)
})
}
}, failureBlock: {
(error: NSError!) in
NSLog("Error!", nil)
})
}
如上所述,应用程序不再崩溃,但未添加 ImageView 并且 UIViewController 仍然是白色 Canvas ......在我的测试中,发生这种情况是因为 Assets 在
assetLib.assetForURL(url, resultBlock: {
(asset: ALAsset!) in
if asset != nil {
...
}
}
为 nil,并且该 block 永远不会执行...我添加了 if asset != nil 条件,因为没有它,应用程序仍然会崩溃。现在,Xcode 揭示了这个问题。回溯:
Attempt to add read-only file at path file:///var/mobile/Media/PhotoData/Photos.sqlite?readonly_shm=1 read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
那么我该如何解决这个问题呢?
最佳答案
enumerateGroupsWithTypes
可以返回 nil
作为组,如文档中指定的那样。您需要处理 nil
情况 - 在组参数之后放置 !
告诉 Swift 如果您尝试使用非 nil 组会使程序崩溃。
修复:
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos)) {
(group: ALAssetsGroup?, stop: CMutablePointer<ObjCBool>) in
if group != nil {
group.setAssetsFilter(ALAssetsFilter.allPhotos())
...
}
}
- 我将
group
设为可选 - 我检查以确保
group
不为零。 - 我没有使用命名的
usingBlock
参数,而是使用 Swift 对尾随闭包的支持来简单地在参数列表之后传递一个 block 。
关于ios - 在 Swift 中使用 ALAsset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24723216/