编辑后将PHAsset保存到相机胶卷

时间:2017-01-29 14:00:07

标签: ios swift uiimage phasset

我已将this框架合并到我的应用中,用于从相机胶卷中选择图像。我的应用程序允许用户选择一个或多个图像,并为图像添加时间戳字符串,并将新图像保存到用户相机胶卷。现在它将编辑后的图像保存为新图像,但我试图弄清楚如何更新用户相机胶卷中的现有图像。我知道我应该可以使用iOS 8发布的Photos框架来做到这一点,但我真的很挣扎。我观看了2014年WWDC会议“介绍照片框架”,但它在Objective-C中,我在翻译时遇到问题。

这是我从相机胶卷中选择图像的代码。我得到了一组PHAssets。

  func selectImageFromCameraRoll(mediaType: String) {
    newMedia = false
    let newImagePicker = BSImagePickerViewController()
    newImagePicker.doneButton = UIBarButtonItem(title: "Stamp", style: UIBarButtonItemStyle.done, target: self, action: nil)

    bs_presentImagePickerController(newImagePicker, animated: true,
                                    select: { (asset: PHAsset) -> Void in
                                      print("Selected")
    }, deselect: { (asset: PHAsset) -> Void in
      print("Deselected")
    }, cancel: { (assets: [PHAsset]) -> Void in
      print("Cancel")
    }, finish: { (assets: [PHAsset]) -> Void in
      for asset in assets {
        self.saveAssetToRoll(asset: asset)
      }
      print("Finished")
    }, completion: nil)
  }

以下是将更新后的图像保存到相机胶卷的代码。

  func saveAssetToRoll(asset: PHAsset) {
    let manager = PHImageManager.default()
    let option = PHImageRequestOptions()
    var pickedImage = UIImage()
    option.isSynchronous = true
    option.deliveryMode = .highQualityFormat
    manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .default, options: option, resultHandler: {(result, info)->Void in
      pickedImage = result!})
    let width = pickedImage.size.width
    let height = pickedImage.size.height
    let location = Locations(rawValue: UserDefaults.standard.value(forKey: "location") as! String)!
    var point = CGPoint(x: 0, y: 0)
    switch location {
    case .topLeft:
      point = CGPoint(x: 30, y: 50)
    case .topRight:
      point = CGPoint(x: width - 30, y: 50)
    case .bottomLeft:
      point = CGPoint(x: 30, y: height - 50)
    case .bottomRight:
      point = CGPoint(x: width - 30, y: height - 50)
    case .center:
      point = CGPoint(x: width / 2, y: height / 2)
    case .topCenter:
      point = CGPoint(x: width / 2, y: 50)
    case .bottomCenter:
      point = CGPoint(x: width / 2, y: height - 50)
    }
    let savedFormat = UserDefaults.standard.value(forKey: "format") as! String
    var date = Date()
    if !currentDateBool {
      date = UserDefaults.standard.value(forKey: "selectedDate") as! Date
    }
    let timestampText = getFormattedDateFromFormatType(formatType: savedFormat, date: date) as NSString
    let timestampImage = textToImage(drawText: timestampText, inImage: pickedImage, atPoint: point)

    UIImageWriteToSavedPhotosAlbum(timestampImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
  }

从阅读Apple的文档看起来我需要使用PHContentEditingInput和PHContentEditingOutput的组合。有人可以指出我正确的方向将其纳入我的代码吗?谢谢!

0 个答案:

没有答案