日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS图片转成视频方法

發布時間:2023/12/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS图片转成视频方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 裁剪圖片** @param image 圖片* @param bounds 大小**/ + (UIImage *)croppedImage:(UIImage *)image bounds:(CGRect)bounds {CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], bounds);UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];CGImageRelease(imageRef);return croppedImage; }+ (UIImage *)clipImage:(UIImage *)image ScaleWithsize:(CGSize)asize {UIImage *newimage;if (nil == image) {newimage = nil;}else{CGSize oldsize = image.size;CGRect rect;if (asize.width/asize.height > oldsize.width/oldsize.height) {rect.size.width = asize.width;rect.size.height = asize.width*oldsize.height/oldsize.width;rect.origin.x = 0;rect.origin.y = (asize.height - rect.size.height)/2;}else{rect.size.width = asize.height*oldsize.width/oldsize.height;rect.size.height = asize.height;rect.origin.x = (asize.width - rect.size.width)/2;rect.origin.y = 0;}UIGraphicsBeginImageContext(asize);CGContextRef context = UIGraphicsGetCurrentContext();CGContextClipToRect(context, CGRectMake(0, 0, asize.width, asize.height));CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background[image drawInRect:rect];newimage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();}return newimage; }+ (CVPixelBufferRef )pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size {NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];CVPixelBufferRef pxbuffer = NULL;CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options, &pxbuffer);NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);CVPixelBufferLockBaseAddress(pxbuffer, 0);void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);NSParameterAssert(pxdata != NULL);CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();CGContextRef context = CGBitmapContextCreate(pxdata, size.width, size.height, 8, 4*size.width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);NSParameterAssert(context);//CGSize drawSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));//BOOL baseW = drawSize.width < drawSize.height;CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);CGColorSpaceRelease(rgbColorSpace);CGContextRelease(context);CVPixelBufferUnlockBaseAddress(pxbuffer, 0);return pxbuffer; }/*** 多張圖片合成視頻**/ - (void)compressImages:(NSArray <UIImage *> *)images completion:(void(^)(NSURL *outurl))block; {//先裁剪圖片NSMutableArray *imageArray = [NSMutableArray array];for (UIImage *image in images){CGRect rect = CGRectMake(0, 0,image.size.width, image.size.height);if (rect.size.width < rect.size.height){rect.origin.y = (rect.size.height - rect.size.width)/2;rect.size.height = rect.size.width;}else{rect.origin.x = (rect.size.width - rect.size.height)/2;rect.size.width = rect.size.height;}//裁剪UIImage *newImage = [videoUtils croppedImage:image bounds:rect];/*** 縮放*/UIImage *finalImage = [videoUtils clipImage:newImage ScaleWithsize:CGSizeMake(640, 640)];[imageArray addObject:finalImage];}NSDate *date = [NSDate date];NSString *string = [NSString stringWithFormat:@"%ld.mov",(unsigned long)(date.timeIntervalSince1970 * 1000)];NSString *cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:string];if ([[NSFileManager defaultManager] fileExistsAtPath:cachePath]){[[NSFileManager defaultManager] removeItemAtPath:cachePath error:nil];}NSURL *exportUrl = [NSURL fileURLWithPath:cachePath];CGSize size = CGSizeMake(640,640);//定義視頻的大小__block AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:exportUrlfileType:AVFileTypeQuickTimeMovieerror:nil];NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,[NSNumber numberWithInt:size.width], AVVideoWidthKey,[NSNumber numberWithInt:size.height], AVVideoHeightKey, nil];AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptorassetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];NSParameterAssert(writerInput);NSParameterAssert([videoWriter canAddInput:writerInput]);if ([videoWriter canAddInput:writerInput])NSLog(@"");elseNSLog(@"");[videoWriter addInput:writerInput];[videoWriter startWriting];[videoWriter startSessionAtSourceTime:kCMTimeZero];//合成多張圖片為一個視頻文件dispatch_queue_t dispatchQueue = dispatch_queue_create("mediaInputQueue", NULL);int __block frame = 0;__weak typeof(self) ws = self;[writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{while ([writerInput isReadyForMoreMediaData]){if(++frame > 8 * 30){[writerInput markAsFinished];//[videoWriter_ finishWriting];if(videoWriter.status == AVAssetWriterStatusWriting){NSCondition *cond = [[NSCondition alloc] init];[cond lock];[videoWriter finishWritingWithCompletionHandler:^{[cond lock];[cond signal];[cond unlock];}];[cond wait];[cond unlock];!block?:block(exportUrl);}break;}CVPixelBufferRef buffer = NULL;int idx = frame/30 * images.count/8;if (idx >= images.count) {idx = images.count - 1;}buffer = (CVPixelBufferRef)[videoUtils pixelBufferFromCGImage:[[imageArray objectAtIndex:idx] CGImage] size:size];if (buffer){if(![adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(frame, 30)]){LPLog(@"fail");}else{LPLog(@"success:%ld",frame);}CFRelease(buffer);}}}];}

總結

以上是生活随笔為你收集整理的iOS图片转成视频方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。