gpt4 book ai didi

iphone - 如何从图像显示视频

转载 作者:行者123 更新时间:2023-11-28 17:39:41 24 4
gpt4 key购买 nike

    #import "ViewController.h"
#import "AVFoundation/AVAssetWriter.h"
#import "AVFoundation/AVAssetWriterInput.h"
#import "AVFoundation/AVMediaFormat.h"
#import "AVFoundation/AVVideoSettings.h"


@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [documentDirectories objectAtIndex: 0];

// NSLog(@"Where it is %@ \n",documentDirectory);
image1.image = [UIImage imageNamed:@"images1.jpg"];
CGSize sizeOfImage = image1.image.size;
// printf("Size of Image width = %f height = %f\n", sizeOfImage.width,sizeOfImage.height);
[self writeImageAsMovie:(UIImage*)image1.image toPath:(NSString*)documentDirectory size:(CGSize)sizeOfImage duration:6];

}


- (void)writeImageAsMovie:(UIImage*)image toPath:(NSString*)path size:(CGSize)size duration:(int)duration
{
NSError *error = nil;

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);


NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];


AVAssetWriterInput *writerInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];




AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];


NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];

//Start a session:
[videoWriter startWriting];

[videoWriter startSessionAtSourceTime:kCMTimeZero];

//Write samples:
//CVPixelBufferRef Utils;
CVPixelBufferRef buffer = [self newPixelBufferFromCGImage:image.CGImage size:size];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];



[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(duration-1, 2)];


//Finish the session:
[writerInput markAsFinished];
[videoWriter endSessionAtSourceTime:CMTimeMake(duration, 2)];



[videoWriter finishWriting];


}

-(CVPixelBufferRef) newPixelBufferFromCGImage:(CGImageRef)image size:(CGSize )frameSize
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];


CVPixelBufferRef pxbuffer = NULL;


CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width, frameSize.height, kCVPixelFormatType_32ARGB,(__bridge CFDictionaryRef)options,&pxbuffer);

NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

status = CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width,
frameSize.height, 8, 4*frameSize.width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGAffineTransform frameTransform;
CGContextConcatCTM(context, frameTransform);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);


return pxbuffer;
}


@end

我是 iOS 新手,我看到一段代码显示堆栈溢出中图像的视频。我运行代码,它没有显示任何错误,但我没有得到所需的输出。我认为错误是这个函数的最后一行

-(void)writeImageAsMovie:(UIImage*)image toPath:(NSString*)path size:(CGSize)size duration:(int)duration) 即 [videoWriter finishWriting]

请帮帮我。

最佳答案

如果您有一个图像数组并且只是想将这些图像作为视频播放,您可以使用 UIImageView 并使用 for 循环将数组中的图像更新为 ImageView 。

如果数组中有图像。喜欢,

for (int i = 0; i < [array count]; i++)
{
imageview.image = (UIImage *)[array objectAtIndex:i];
}

关于iphone - 如何从图像显示视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8635036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com