gpt4 book ai didi

image - 快速将图像保存到特殊相册

转载 作者:行者123 更新时间:2023-11-28 05:33:42 24 4
gpt4 key购买 nike

我想在某个相册中添加一张新照片,我正在尝试这样做:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

albumName = "\(data.objectAtIndex(indexPath.row))"

let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self

let actionSheet = UIAlertController(title: "Choose image source", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

actionSheet.addAction(UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: {})

}))

actionSheet.addAction(UIAlertAction(title: "Camera Roll", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(imagePickerController, animated: true, completion: nil)

}))

actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

self.presentViewController(actionSheet, animated: true, completion: nil)

}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage

var groupToAddTo: ALAssetsGroup = ALAssetsGroup()

self.library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum),
usingBlock: {
(group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumName){
groupToAddTo = group
}
},
failureBlock: {
(myerror: NSError!) -> Void in
println("error occurred: \(myerror.localizedDescription)")
})

var img: CGImageRef = image.CGImage

self.library.writeImageToSavedPhotosAlbum(img, metadata: nil, completionBlock: {
(assetUrl: NSURL!, error: NSError!) -> Void in
if error.code == 0 {
println("saved image completed: \(assetUrl)")

self.library.assetForURL(assetUrl, resultBlock: { (asset: ALAsset!) -> Void in
groupToAddTo.addAsset(asset)
return
}, failureBlock: {
(myerror: NSError!) -> Void in
println("error occurred: \(myerror.localizedDescription)")
})
} else {
println("saved image failed. \(error.localizedDescription) code \(error.code)")
}
} )




}

但是,当我运行这段代码时,出现了这个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

在这一行中:

if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumName){

那么问题来了,如何在swift中将图片保存到某个相册?

最佳答案

我打赌 albumName 是一个可选值。

你应该放一个if语句来判断almbumName是否为nil。当它为 nil 时,在您的 iPhone 上创建一个新专辑名称。

关于image - 快速将图像保存到特殊相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com