2014-06-18 8 views
6

मैं चित्र लेने के लिए तेजी से AVFoundation का उपयोग कर रहा हूं लेकिन मैं उद्देश्य सी से स्विफ्ट तक कोड की किसी भी func लाइनों को परिवर्तित नहीं कर सकता। मेरे समारोह कोड है:कोड AVFoundation उद्देश्य सी को स्विफ्ट कैसे परिवर्तित करें?

- (void) capImage { //method to capture image from AVCaptureSession video feed 
AVCaptureConnection *videoConnection = nil; 
for (AVCaptureConnection *connection in stillImageOutput.connections) { 

    for (AVCaptureInputPort *port in [connection inputPorts]) { 

     if ([[port mediaType] isEqual:AVMediaTypeVideo]) { 
      videoConnection = connection; 
      break; 
     } 
    } 

    if (videoConnection) { 
     break; 
    } 
} 

NSLog(@"about to request a capture from: %@", stillImageOutput); 
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 

    if (imageSampleBuffer != NULL) { 
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
     [self processImage:[UIImage imageWithData:imageData]]; 
    } 
}]; 

}

यह पंक्ति मुझे त्रुटि AnyObject [] प्रोटोकॉल sequencfe के अनुरूप नहीं है भेजने ..:

for (AVCaptureInputPort *port in [connection inputPorts]) { 

तेज में:

for port:AnyObject in connection.inputPorts { 

और मुझे नहीं पता कि इस लाइन को कैसे परिवर्तित करें:

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 

क्या आप मुझे तेजी से बदलने में मदद कर सकते हैं? धन्यवाद !!

AnyObject की
+4

जानने के लिए प्रयास करें पूरा करने वाले हैंडलर के बारे में अधिक जानकारी और उन्हें त्वरित रूप से परिवर्तित करने के लिए इस प्रश्न को देखें http://stackoverflow.com/questions/24190277/writing-handler-f या-uialertaction/24190415 # 24190415 – jbman223

उत्तर

6
for (AVCaptureInputPort *port in [connection inputPorts]) {) 

सरणी इस तरह, interating से पहले अपने वास्तविक प्रकार की सरणियों के लिए डाली जानी चाहिए:

for (port in connection.inputPorts as AVCaptureInputPort[]) { } 

बंद करने के लिए ब्लॉक के संदर्भ में, तुम सिर्फ वाक्य रचना सही पाने के लिए किया है।

stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { 
    (imageSampleBuffer, error) in // This line defines names the inputs 
    //... 
} 

ध्यान दें कि यह Trailing Closure Syntax का भी उपयोग करता है। दस्तावेज़ों पर और पढ़ें!

संपादित करें: initializers के संदर्भ में, वे अब इस तरह दिखना:

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer) 
self.processImage(UIImage(data:imageData)) 
+0

और यह ?? NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataReresentation: imageSampleBuffer]; [स्वयं प्रक्रिया छवि: [UIImage imageWithData: imageData]]; तेजी से कैसे पहुंचे, मुझे नहीं पता कि कैसे व्यापार, धन्यवाद! – user3745888

+0

@ user3745888 संपादित करें – Jack

+0

महान !! लेकिन इस विधि में अन्य त्रुटियों के समान त्रुटियां हैं जिनके पास उद्देश्य सी में हैं: CGImageRef imageRef = CGImageCreateWithImageInRect ([smallImage CGImage], cropRect); [कब्जा छवि सेट इमेज: [UIImage imageWithCGImage: imageRef]]; और तेज़: imageRef = CGImageCreateWithImageInRect (छोटा Image? CGImage !, cropRect) और त्रुटियों का प्रकार प्राप्त करें .. imageView2.image.imageWithAlignmentRectInsets (imageRef) धन्यवाद! – user3745888

0

यह बंदरगाहों के साथ समस्या का जवाब देना चाहिए:

if let videoConnection = stillImageOuput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here} 
1

इस

var videoConnection :AVCaptureConnection? 
    if let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){ 
    self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (buffer:CMSampleBuffer!, error: NSError!) -> Void in 
    if let exifAttachments = CMGetAttachment(buffer, kCGImagePropertyExifDictionary, nil) { 
     let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer) 
     self.previewImage.image = UIImage(data: imageData) 
     UIImageWriteToSavedPhotosAlbum(self.previewImage.image, nil, nil, nil) 
     } 
    }) 
    } 
संबंधित मुद्दे