这个问题有很多部分。
如何更改此代码,拍摄多张照片(例如10张)并将它们放入数组并将其显示为gif?
我“有点”这样做,但是分开;一个程序捕获常规照片并将其保存到相机胶卷(CAMERA),另一个程序拍摄已保存在Xcode项目中的照片,将它们放入一个数组并将它们显示为gif(GIF)。
以下是常规相机代码:
#CAMERA
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var captureSession : AVCaptureSession?
var stillImageOutput : AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
@IBOutlet var cameraView: UIView!
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.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
previewLayer?.frame = cameraView.bounds
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error : NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError {
error = error1
input = nil
}
if (error == nil && captureSession?.canAddInput(input) != nil){
captureSession?.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
if (captureSession?.canAddOutput(stillImageOutput) != nil){
captureSession?.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
cameraView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
}
}
@IBOutlet var tempImageView: UIImageView!
func didPressTakePhoto(){
if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
(sampleBuffer, error) in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProviderCreateWithCFData(imageData)
let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
self.tempImageView.image = image
self.tempImageView.hidden = false
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
})
}
}
var didTakePhoto = Bool()
func didPressTakeAnother(){
if didTakePhoto == true{
tempImageView.hidden = true
didTakePhoto = false
}
else{
captureSession?.startRunning()
didTakePhoto = true
didPressTakePhoto()
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
didPressTakeAnother()
}
}
以下是GIF代码:
#GIF
-(IBAction)play{
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"IMG_5331.JPG"],
[UIImage imageNamed:@"IMG_5332.JPG"],
[UIImage imageNamed:@"IMG_5333.JPG"],
[UIImage imageNamed:@"IMG_5334.JPG"],
[UIImage imageNamed:@"IMG_5335.JPG"],
[UIImage imageNamed:@"IMG_5336.JPG"],
[UIImage imageNamed:@"IMG_5337.JPG"],
[UIImage imageNamed:@"IMG_5338.JPG"],
[UIImage imageNamed:@"IMG_5339.JPG"],
[UIImage imageNamed:@"IMG_5340.JPG"],nil];
[image setAnimationRepeatCount:-1];
image.animationDuration = 1;
[image startAnimating];
}
-(IBAction)stop{
[image stopAnimating];
}