【问题标题】:Print image on top of saved photo (swift3)在保存的照片上打印图像(swift3)
【发布时间】:2017-02-19 21:23:38
【问题描述】:

现在我的代码只是拍摄一张照片并将其保存到照片库中。我想在照片右下角的照片上打印一张照片。该照片名为“logo.png”。这就像有线电视新闻的三分之一。

import UIKit
  import AVFoundation

  class ViewController: UIViewController,AVCapturePhotoCaptureDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate  {

var captureSesssion : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!

@IBOutlet var sa: UIView!
     override func viewDidLoad() {
super.viewDidLoad()



captureSesssion = AVCaptureSession()
captureSesssion.sessionPreset = AVCaptureSessionPresetPhoto
cameraOutput = AVCapturePhotoOutput()

let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

if let input = try? AVCaptureDeviceInput(device: device) {
    if (captureSesssion.canAddInput(input)) {
        captureSesssion.addInput(input)
        if (captureSesssion.canAddOutput(cameraOutput)) {
            captureSesssion.addOutput(cameraOutput)
            previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
                previewLayer.frame = CGRect(x: 10, y: 10, width: 700, height: 700)
            sa.layer.addSublayer(previewLayer)
            captureSesssion.startRunning()
        }}}}


      @IBAction func didPressTakePhoto(_ sender: UIButton) {



      let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [
    kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
    kCVPixelBufferWidthKey as String: 160,
    kCVPixelBufferHeightKey as String: 160
]
settings.previewPhotoFormat = previewFormat
cameraOutput.capturePhoto(with: settings, delegate: self)


}


func imagex(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {

}

func capture(_ captureOutput: AVCapturePhotoOutput,  didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?,  previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings:  AVCaptureResolvedPhotoSettings, bracketSettings:   AVCaptureBracketedStillImageSettings?, error: Error?) {




    if  let sampleBuffer = photoSampleBuffer,
        let previewBuffer = previewPhotoSampleBuffer,
        let dataImage =  AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer:  sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {


        let dataProvider = CGDataProvider(data: dataImage as CFData)
        let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
        let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

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

【问题讨论】:

    标签: ios swift3 save photo layer


    【解决方案1】:

    您必须使用图形上下文。这是我在笔记上的一个通用示例。这对于组合图像很有用,只需对其进行调整以使其按您的意愿工作(即在右上角设置您的自定义图像(在我的情况下为 image1)。)

    var image1 = UIImage(named: "image1.png")
    var imageBase = yourImageView.image
    
    var size = yourImageView.frame.size
    UIGraphicsBeginImageContext(size)
    
    let areaSize = CGRect(x: 0, y: size.width - ( size.width / 4 ), width: (size.width / 4), height:( size.height / 4) )
    image1!.drawInRect(areaSize)
    
    imageBase!.drawInRect(areaSize, blendMode: CGBlendMode.Normal, alpha: 0.8)
    
    var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    

    【讨论】:

    • 我会将它放在 imagex 或 capture 中的什么功能? @juanCurti
    • 是一样的。当您确定图像已完美保存时,您应该编写它。
    • 所以不管它是在捕获还是在 imagex 中,它只需要在这些功能之一中。 @juanCurti
    • 我不知道你的应用程序的流程,这取决于。如果在照片保存到照片库后调用 didFinishSavingWithError ,我会写在那个里面
    • 问题出在我的代码上,我什至不知道哪一行代码正在保存图像。此代码从直接 uiview 转到照片库。你知道这段代码的哪一行保存了照片吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    相关资源
    最近更新 更多