2017-02-19 71 views
1

現在,我的代碼只需拍攝一張照片並將其保存到照片庫中。我想在照片右下角的照片上打印一張照片。這張照片被稱爲「logo.png」。這就像有線電視新聞中的三分之一一樣。在已保存的照片上打印圖像(swift3)

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) 
     } } } 

回答

0

您必須使用圖形上下文。這是我筆記中的一個通用示例。這是非常有用的合成圖像,只是適應它做你想要它的工作(這將是在右上角設置您的自定義圖像(在我的情況下圖像1)。)

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() 
+0

我會把這有什麼功能imagex還是捕獲? @juanCurti –

+0

這是一樣的。您應該在確定圖像完美保存後寫下。 –

+0

因此,如果它只是在捕獲或imagex中,只需要處理其中一個函數,那並不重要。 @juanCurti –