- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在这里需要帮助,已经修复了几个小时但无济于事。
我想捕捉图像并显示它的预览,在 AVCaptureVideoPreviewLayer 中,它以横向模式显示。然而,当我抓取图像时,它显示为纵向模式。
我不知道为什么,我希望有人能帮助我:)
感谢阅读。
实时取景时
当捕获并显示在 uiimageview 中时
- (void)addStillImageOutput
{
[self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
[[self stillImageOutput] setOutputSettings:outputSettings];
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[[self captureSession] addOutput:[self stillImageOutput]];
}
- (void)captureStillImage
{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if([videoConnection isVideoOrientationSupported])
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft ) {
//NSLog(@"2222UIInterfaceOrientationLandscapeLeft");
[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
}
if (orientation == UIInterfaceOrientationLandscapeRight )
{//NSLog(@"222UIInterfaceOrientationLandscapeRight");
[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}
}
if (videoConnection) {
break;
}
}
//NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments) {
////NSLog(@"attachements: %@", exifAttachments);
}
else
{
//NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
//NSData *jpgImageData = UIImageJPEGRepresentation(image, 0.9);
//UIImage *jpgImage = [[UIImage alloc]initWithData:jpgImageData];
[self setStillImage:image];
self.imageExifDict = (__bridge NSDictionary *)exifAttachments;
////NSLog(@"NSdictionary from CFDict %@",self.imageExifDict);
//
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
}];
}
我将图像从 NSData 传递到 UIImage
self.capturedImage.image = [[self captureManager] stillImage];
最佳答案
我用这个 C 函数做了:
UIImage *scaleAndRotateImage(UIImage *image)
{
int kMaxResolution = 2048; // Or whatever
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
CGFloat ratio = width/height;
if (ratio > 1) {
bounds.size.width = kMaxResolution;
bounds.size.height = bounds.size.width / ratio;
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = bounds.size.height * ratio;
}
}
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch(orient) {
case UIImageOrientationUp: //EXIF = 1
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight: //EXIF = 8
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
这个:
int kMaxResolution = 2048; // Or whatever
因为我用那个分辨率的新 Ipad
关于iphone - 如何在横向模式而不是纵向模式下捕捉 avfoundation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925631/
我有一个 AVMutableVideoComposition,我正在使用 AVAssetExportSession 导出它,我正在尝试覆盖一个每秒更新一次的自定义时间戳。我应该使用 AVAssetWr
我是 MAC OSX 开发的新手。我想在 OSX 10.7 上使用 AVFoundation 将视频捕获为原始帧。我不明白为相机设备设置特定的视频分辨率,不知何故我使用 VideoSettings 设
使用 AVFoundation/QTKit 如何将多个视频设备同时录制到一个文件中? 我知道如何分别记录它们,但是尝试同时记录它们会导致诸如... “无法添加到 session 中,因为源和目标媒体类
我有一个关于 CoreAudio 和 AVFoundation 的问题。 我使用 CoreAudio 和 AUGraph 和 AudioUnit 构建了一个专业音频应用程序。 我想切换到看起来非常棒的
Apple Watch 能用吗AVFoundation ?更具体地说,可以AVAudioPlayer和 AVAudioRecorder工作? 我正在尝试制作一个应用程序,让您可以将声音录制到 Appl
Apple Watch 可以使用 AVFoundation 吗?更具体地说,AVAudioPlayer 和 AVAudioRecorder 可以工作吗? 我正在尝试制作一个应用程序,让您可以将声音录制
当我创建一个 iOS 项目和“Build Phases -> Link binary with Libraries”时,我添加了 AVFoundation.framework 库并使用 #import
我正在尝试创建一个格式正确的视频文件,用于 Apple 的 HTTP Live Streaming。这是创建文件的代码: // Init the device inputs AVCaptureDevi
我检查了答案,但找不到任何代码..我还登录以查看是否丢失了某些东西,但两个Logg都出现了,但声音没有播放...我也导入了 框架...我不知道我在做什么错我是新手,请帮忙...谢谢 - (void)v
对于这个问题,我只询问在没有外部库的情况下使用 Xcode 和 iOS 的可能性。我已经在探索在另一个 question 中使用 libtiff 的可能性. 问题 数周以来,我一直在筛选堆栈溢出,并为
我正在尝试采用不同的方法来组合视频。我正在为每个转换创建一个新轨道。 此代码的问题在于显示了第一个视频,而其他所有视频都是黑色的。 整个片段的音频覆盖都是正确的。看起来视频没有带入合成中,因为文件的大
回到使用AVAudioPlayer时,使用currentPosition来获取并设置位置,但是当使用AVAudioEngine和AVAudioPlayerNode时会使用什么呢? 最佳答案 AVAud
有人可以帮我将一个图标合并到一个视频中,该视频应该在视频播放时摆动。我已将动画添加到摆动图标,但只有静态图标会合并到视频中。如果有人可以帮助我,我将不胜感激。 我给你我正在使用的代码 //- (voi
Example 你好, 努力旋转此视频以显示在正确的方向并填满整个屏幕。 我无法使用 videocompisition 获取 avasset,但无法使其正常工作。 let videoAsset
如何一次在屏幕上显示多个相机预览? 如果我开始一个新的 AVCaptureSession,另一个就会停止。如果我使用与另一个 session 相同的 session 初始化一个新的 AVCapture
以下是我的录制按钮方法的内容。它创建了所需的文件,但无论我记录多长时间,创建的文件的长度始终为 4kb 和 0 秒,我无法弄清楚为什么它无效。对于averagePowerPerChannel,计量也返
我已经研究这个太久了。 我正在尝试获取 MacOS 网络摄像头数据并在网络摄像头输出的帧上运行 CIDetect。 我知道我需要: 连接 AVCaptureDevice (如输入)到AVCapture
我正在尝试从 CMSampleBufferRef 检索 CVPixelBufferRef,以便更改 CVPixelBufferRef 以动态覆盖水印。 我正在使用 CMSampleBufferGetI
捕获图像时,输出图像的颜色与我在预览图层上看到的颜色不同。出于某种原因,颜色略有变化。有没有人遇到过这个问题?我怎样才能解决这个问题? 当我从 didFinishProcessingPhotoSamp
对于 iOS5 来说可能是一个简单的问题他们将 AVFoundation 的eekToTime 方法更改为如下所示: [avPlayer seekToTime:startTime toleranceB
我是一名优秀的程序员,十分优秀!