所以我一整天都沒有運氣,它不必說很沮喪,我查了很多例子和可下載的類別,都宣稱能夠完美地裁剪圖像。他們這樣做,然而,當我嘗試從通過AVCaptureSession生成的圖像執行它時,它不起作用。我諮詢這兩種來源圖像裁剪AVCaptureSession圖像
http://codefuel.wordpress.com/2011/04/22/image-cropping-from-a-uiscrollview/
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
,並從第一個鏈接的項目似乎爲標榜,但只要我本事做一個AV捕捉圖像上相同的魔術,直接工作。 ..沒有...
有沒有人有這方面的見解?這裏也是我的代碼供參考。
- (IBAction)TakePhotoPressed:(id)sender
{
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)
{
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
//NSLog(@"attachements: %@", exifAttachments);
}
else
NSLog(@"no attachments");
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
NSLog(@"%f",image.size.width);
NSLog(@"%f",image.size.height);
float scale = 1.0f/_scrollView.zoomScale;
CGRect visibleRect;
visibleRect.origin.x = _scrollView.contentOffset.x * scale;
visibleRect.origin.y = _scrollView.contentOffset.x * scale;
visibleRect.size.width = _scrollView.bounds.size.width * scale;
visibleRect.size.height = _scrollView.bounds.size.height * scale;
UIImage* cropped = [self cropImage:image withRect:visibleRect];
[croppedImage setImage:cropped];
[image release];
}
];
[croppedImage setHidden:NO];
}
上面使用的cropImage函數。
-(UIImage*)cropImage :(UIImage*)originalImage withRect :(CGRect) rect
{
CGRect transformedRect=rect;
if(originalImage.imageOrientation==UIImageOrientationRight)
{
transformedRect.origin.x = rect.origin.y;
transformedRect.origin.y = originalImage.size.width-(rect.origin.x+rect.size.width);
transformedRect.size.width = rect.size.height;
transformedRect.size.height = rect.size.width;
}
CGImageRef cr = CGImageCreateWithImageInRect(originalImage.CGImage, transformedRect);
UIImage* cropped = [UIImage imageWithCGImage:cr scale:originalImage.scale orientation:originalImage.imageOrientation];
[croppedImage setFrame:CGRectMake(croppedImage.frame.origin.x,
croppedImage.frame.origin.y,
cropped.size.width,
cropped.size.height)];
CGImageRelease(cr);
return cropped;
}
我也動心了冗長和武裝的任何人可以幫助我在我的困境與儘可能多的信息可以張貼我的我的滾動視圖和avcapture會話中運行init。但是,這可能有點太多,所以如果你想看到它只是問。
現在對於什麼樣的代碼實際上做的結果嗎?..
它看起來像什麼我拍攝照片之前
後...
編輯:
嗯,我現在有幾個意見,沒有評論,所以要麼沒有人知道它或它是如此簡單,他們認爲我會再次想出來......在任何情況下,我還沒有取得任何進展。因此,對於任何有興趣在這裏是用一小段代碼示例應用程序的所有設置,你可以看到我在做什麼
https://docs.google.com/open?id=0Bxr4V3a9QFM_NnoxMkhzZTVNVEE
我面臨同樣的問題。你能告訴我在哪裏需要調用這段代碼片段?我會很感激。 –
這方面的任何更新?我有完全一樣的問題。看起來,截圖的質量不會像從AV捕獲會話中獲得的照片那麼好。謝謝。 – seenickcode