gpt4 book ai didi

swift - 保存图片中的图片应用程序

转载 作者:行者123 更新时间:2023-11-30 13:36:10 25 4
gpt4 key购买 nike

我制作了一个 iPhone 应用程序,用户可以在其中拍照。我需要在代码中添加什么,以便当用户拍照时将照片保存到用户库中?

下面是我到目前为止的代码

导入UIKit导入AVFoundation

ViewController 类:UIViewController、UIImagePickerControllerDelegate、UINavigationControllerDelegate {

var picker = UIImagePickerController()

@IBOutlet var camera: UIButton!



@IBAction func camera(sender: UIButton) {


if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = true

//customView stuff
let customViewController = CustomOverlayViewController(
nibName:"CustomOverlayViewController",
bundle: nil
)


let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
customView.frame = self.picker.view.frame

customView.cameraLabel.text = "Hello Cute Camera"


picker.modalPresentationStyle = .FullScreen
presentViewController(picker,animated: true,completion: {
self.picker.cameraOverlayView = customView
}
)




} else { //no camera found -- alert the user.
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}


//MARK: Picker Delegates
func imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true,
completion: nil)
}




}






override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

最佳答案

这就是我在我的应用程序中所做的,这可能会有所帮助。

func saveImage(image: UIImage, funParam: (Bool) -> ()) ->Bool { //, metadata: NSDictionary) {
if assetCollection == nil {
return false // if there was an error upstream, skip the save
}

//self.validateStatus()

//TODO: the return false is happening before the photo is save.
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
albumChangeRequest!.addAssets([assetPlaceHolder!])
}, completionHandler: { (success, error) -> Void in
if success {
funParam(true)
}
else {
funParam(false)
}

})
return true

}

参数如下:

  1. 图像是您要保护的图像。

  2. funParam 是一个函数,如果您想同步控制图像的保存,它将成为完成处理程序。我这样做因为实际保存图像的代码是异步运行的。

已更新消息以及通话示例:

以下是我如何调用此代码的示例:

我有一个名为 PhotoAlbum.swif 的类,我这样调用该函数:

let validate = self.saveImage(imageView.image!, funParam: getSaveResponse)

getSaveResponse 是一种控制图像安全后应该发生的情况的方法。

关于swift - 保存图片中的图片应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050394/

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