{"id":849,"date":"2023-05-30T12:05:05","date_gmt":"2023-05-30T04:05:05","guid":{"rendered":"https:\/\/app.60day.cn\/appday\/?p=849"},"modified":"2023-05-30T12:05:05","modified_gmt":"2023-05-30T04:05:05","slug":"ios%e5%bd%95%e5%b1%8f%e5%bc%80%e5%8f%91%e6%9c%89%e9%82%a3%e4%ba%9b%e9%9a%be%e7%82%b9%e5%92%8c%e6%b3%a8%e6%84%8f%e7%82%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/app.60day.cn\/appday\/kaifaapp\/849\/","title":{"rendered":"ios\u5f55\u5c4f\u5f00\u53d1\u6709\u90a3\u4e9b\u96be\u70b9\u548c\u6ce8\u610f\u70b9\uff1f"},"content":{"rendered":"<p>iOS\u5f55\u5c4f\u5f00\u53d1\u662f\u6307\u5728iOS\u8bbe\u5907\u4e0a\u5b9e\u73b0\u5f55\u5236\u5c4f\u5e55\u89c6\u9891\u7684\u529f\u80fd\u3002\u5728iOS 11\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u82f9\u679c\u516c\u53f8\u5df2\u7ecf\u539f\u751f\u652f\u6301\u4e86\u5c4f\u5e55\u5f55\u5236\u529f\u80fd\uff0c\u4e0d\u8fc7\u5728\u4e00\u4e9b\u5b9a\u5236\u5316\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u81ea\u5df1\u5f00\u53d1\u5f55\u5c4f\u529f\u80fd\u3002<\/p>\n<p>\u4e00\u3001\u539f\u7406<\/p>\n<p>iOS\u5f55\u5c4f\u7684\u539f\u7406\u662f\u901a\u8fc7\u6355\u6349\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e\uff0c\u7136\u540e\u5c06\u56fe\u50cf\u6570\u636e\u7f16\u7801\u6210\u89c6\u9891\uff0c\u6700\u540e\u5c06\u89c6\u9891\u4fdd\u5b58\u5230\u672c\u5730\u6216\u4e0a\u4f20\u5230\u4e91\u7aef\u3002\u5177\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>1. \u6355\u6349\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e<\/p>\n<p>iOS\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a UIScreen \u7684\u7c7b\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\u7684\u5c4f\u5e55\u5bf9\u8c61\uff0c\u901a\u8fc7\u8be5\u5bf9\u8c61\u5373\u53ef\u83b7\u53d6\u5230\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<p>2. \u7f16\u7801\u56fe\u50cf\u6570\u636e\u4e3a\u89c6\u9891<\/p>\n<p>\u5c06\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e\u7f16\u7801\u6210\u89c6\u9891\u9700\u8981\u4f7f\u7528\u5230 AVFoundation \u6846\u67b6\u4e2d\u7684 AVAssetWriter \u7c7b\u3002\u8be5\u7c7b\u53ef\u4ee5\u5c06\u6355\u83b7\u5230\u7684\u56fe\u50cf\u6570\u636e\u7f16\u7801\u6210 H.264 \u683c\u5f0f\u7684\u89c6\u9891\u3002<\/p>\n<p>3. \u4fdd\u5b58\u89c6\u9891\u5230\u672c\u5730\u6216\u4e0a\u4f20\u5230\u4e91\u7aef<\/p>\n<p>\u6700\u540e\uff0c\u5c06\u7f16\u7801\u540e\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u672c\u5730\u6216\u4e0a\u4f20\u5230\u4e91\u7aef\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684 API \u6216\u7b2c\u4e09\u65b9\u5e93\u5b9e\u73b0\u3002<\/p>\n<p>\u4e8c\u3001\u8be6\u7ec6\u4ecb\u7ecd<\/p>\n<p>1. \u6355\u6349\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e<\/p>\n<p>\u5728 iOS \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 UIScreen \u7c7b\u83b7\u53d6\u5230\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\u7684\u5c4f\u5e55\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u5bf9\u8c61\u5373\u53ef\u83b7\u53d6\u5230\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>UIScreen *mainScreen = [UIScreen mainScreen];<\/p>\n<p>CGRect screenRect = mainScreen.bounds;<\/p>\n<p>UIGraphicsBeginImageCont<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/15\/18515473874.jpg\" \/><\/figure>\n<\/p>\n<p>ext(screenRect.size);<\/p>\n<p>CGContextRef ctx = UIGraphicsGetCurrentContext();<\/p>\n<p>[mainScreen snapshotViewAfterScreenUpdates:NO].layer renderInContext:ctx];<\/p>\n<p>UIImage *image = UIGraphicsGetImageFromCurrentImageContext();<\/p>\n<p>UIGraphicsEndImageContext();<\/p>\n<p>&#8220;`<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u5230 UIScreen \u5b9e\u4f8b\uff0c\u7136\u540e\u83b7\u53d6\u5230\u5c4f\u5e55\u7684\u5927\u5c0f\uff0c\u63a5\u7740\u5f00\u542f\u4e00\u4e2a\u56fe\u5f62\u4e0a\u4e0b\u6587\uff0c\u5c06\u5c4f\u5e55\u7684\u56fe\u50cf\u7ed8\u5236\u5230\u56fe\u5f62\u4e0a\u4e0b\u6587\u4e2d\uff0c\u6700\u540e\u83b7\u53d6\u5230\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<p>2. \u7f16\u7801\u56fe\u50cf\u6570\u636e\u4e3a\u89c6\u9891<\/p>\n<p>\u5c06\u6355\u83b7\u5230\u7684\u56fe\u50cf\u6570\u636e\u7f16\u7801\u6210\u89c6\u9891\u9700\u8981\u4f7f\u7528\u5230 AVFoundation \u6846\u67b6\u4e2d\u7684 AVAssetWriter \u7c7b\u3002\u5728\u4f7f\u7528 AVAssetWriter \u7c7b\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a AVAssetWriter \u5b9e\u4f8b\uff0c\u5e76\u8bbe\u7f6e\u597d\u8f93\u51fa\u8def\u5f84\u3001\u89c6\u9891\u5927\u5c0f\u3001\u7f16\u7801\u683c\u5f0f\u7b49\u53c2\u6570\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>NSString *videoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@&#8221;test.mp4&#8243;];<\/p>\n<p>NSURL *videoUrl = [NSURL fileURLWithPath:videoPath];<\/p>\n<p>AVAssetWriter *assetWriter = [[AVAssetWriter alloc] initWithURL:videoUrl fileType:AVFileTypeMPEG4 error:nil];<\/p>\n<p>NSDictionary *outputSettings = @{<\/p>\n<p>    AVVideoCodecKey: AVVideoCodecH264,<\/p>\n<p>    AVVideoWidthKey: @(screenRect.size.width),<\/p>\n<p>    AVVideoHeightKey: @(screenRect.size.height)<\/p>\n<p>};<\/p>\n<p>AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];<\/p>\n<p>assetWriterInput.expectsMediaDataInRealTime = YES;<\/p>\n<p>[assetWriter addInput:assetWriterInput];<\/p>\n<p>&#8220;`<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a AVAssetWriter \u5b9e\u4f8b\uff0c\u6307\u5b9a\u4e86\u8f93\u51fa\u8def\u5f84\u548c\u6587\u4ef6\u7c7b\u578b\u3002\u7136\u540e\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u7f16\u7801\u53c2\u6570\uff0c\u5305\u62ec\u7f16\u7801\u683c\u5f0f\u3001\u89c6\u9891\u5927\u5c0f\u7b49\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a AVAssetWriterInput \u5b9e\u4f8b\uff0c\u6307\u5b9a\u4e86\u5a92\u4f53\u7c7b\u578b\u4e3a\u89c6\u9891\uff0c\u8f93\u51fa\u53c2\u6570\u4e3a\u4e0a\u9762\u8bbe\u7f6e\u7684\u7f16\u7801\u53c2\u6570\uff0c\u540c\u65f6\u8bbe\u7f6e expectsMediaDataInRealTime \u5c5e\u6027\u4e3a YES\uff0c\u8868\u793a\u8f93\u5165\u6570\u636e\u662f\u5b9e\u65f6\u7684\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06 AVAssetWriterInput \u5b9e\u4f8b\u6dfb\u52a0\u5230 AVAssetWriter \u5b9e\u4f8b\u4e2d\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6355\u83b7\u5230\u7684\u56fe\u50cf\u6570\u636e\u5199\u5165\u5230 AVAssetWriterInput \u5b9e\u4f8b\u4e2d\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>CGImageRef imageRef = image.CGImage;<\/p>\n<p>CMTime presentationTime = CMTimeMake(frameCount, 30);<\/p>\n<p>CVPixelBufferRef pixelBuffer = NULL;<\/p>\n<p>CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault, screenRect.size.width, sc<a href=\"https:\/\/www.yimenapp.net\/dabao-4090.html\">\u4e00\u952e\u751f\u6210android<\/a>reenRect.size.height, kCVPixelFormatType_32ARGB, (__bri<a href=\"https:\/\/www.yimenapp.net\/dabao-2151.html\">ios\u5e94\u7528\u62c6\u5305<\/a>dge CFDictionaryRef) @{<\/p>\n<p>    (__bridge NSString *)kCVPixelBufferIOSurfacePropertiesKey: @{},<\/p>\n<p>}, &amp;pixelBuffer);<\/p>\n<p>CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(pixelBuffer), CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer), 8, CVPixelBufferGetBytesPerRow(pixelBuffer), CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);<\/p>\n<p>CGContextDrawImage(context, CGRectMake(0, 0, screenRect.size.width, screenRect.size.height), imageRef);<\/p>\n<p>CGContextRelease(context);<\/p>\n<p>CVPixelBufferLockBaseAddress(pixelBuffer, 0);<\/p>\n<p>[assetWriterInput appendSampleBuffer:[self sampleBufferFromPixelBuffer:pixelBuffer presentationTime:presentationTime]];<\/p>\n<p>CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);<\/p>\n<p>CVPixelBufferRelease(pixelBuffer);<\/p>\n<p>&#8220;`<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u56fe\u50cf\u6570\u636e\u8f6c\u6362\u6210 CVPixelBufferRef \u7c7b\u578b\u7684\u6570\u636e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u901a\u8fc7 CGContextRef \u5c06\u56fe\u50cf\u6570\u636e\u7ed8\u5236\u5230 CVPixelBufferRef \u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06 CVPixelBufferRef \u4e2d\u7684\u6570\u636e\u8f6c\u6362\u6210 CMSampleBufferRef \u7c7b\u578b\uff0c\u901a\u8fc7 AVAssetWriterInput \u5b9e\u4f8b\u5c06\u6570\u636e\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>3. \u4fdd\u5b58\u89c6\u9891\u5230\u672c\u5730\u6216\u4e0a\u4f20\u5230\u4e91\u7aef<\/p>\n<p>\u5c06\u7f16\u7801\u540e\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u672c\u5730\u6216\u4e0a\u4f20\u5230\u4e91\u7aef\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684 API \u6216\u7b2c\u4e09\u65b9\u5e93\u5b9e\u73b0\u3002<\/p>\n<p>\u5982\u679c\u8981\u5c06\u89c6\u9891\u4fdd\u5b58\u5230\u672c\u5730\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>[assetWriter finishWritingWithCompletionHandler:^{<\/p>\n<p>    NSLog(@&#8221;\u89c6\u9891\u5df2\u4fdd\u5b58\u5230\u672c\u5730&#8221;);<\/p>\n<p>}];<\/p>\n<p>&#8220;`<\/p>\n<p>\u5982\u679c\u8981\u5c06\u89c6\u9891\u4e0a\u4f20\u5230\u4e91\u7aef\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982\u963f\u91cc\u4e91 OSS\uff0c\u5c06\u89c6\u9891\u4e0a\u4f20\u5230\u4e91\u7aef\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>[[OSSClient sharedInstance] asyncUploadData:data<\/p>\n<p>                                     bucketName:@&#8221;bucketName&#8221;<\/p>\n<p>                                    objectKey:@&#8221;objectKey&#8221;<\/p>\n<p>                            completionHandler:^(BOOL isSuccess, NSError *error) {<\/p>\n<p>    if (isSuccess) {<\/p>\n<p>        NSLog(@&#8221;\u89c6\u9891\u5df2\u4e0a\u4f20\u5230\u4e91\u7aef&#8221;);<\/p>\n<p>    } else {<\/p>\n<p>        NSLog(@&#8221;\u89c6\u9891\u4e0a\u4f20\u5931\u8d25\uff0c\u9519\u8bef\u4fe1\u606f\uff1a%@&#8221;, error.localizedDescription);<\/p>\n<p>    }<\/p>\n<p>}];<\/p>\n<p>&#8220;`<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>iOS\u5f55\u5c4f\u5f00\u53d1\u9700\u8981\u4f7f\u7528\u5230 UIScreen\u3001AVFoundation \u7b49\u6846\u67b6\uff0c\u901a\u8fc7\u6355\u6349\u5c4f\u5e55\u4e0a\u7684\u56fe\u50cf\u6570\u636e\uff0c\u5c06\u56fe\u50cf\u6570\u636e\u7f16\u7801\u6210\u89c6\u9891\uff0c\u6700\u540e\u5c06\u89c6\u9891\u4fdd\u5b58\u5230\u672c\u5730\u6216\u4e0a\u4f20\u5230\u4e91\u7aef\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u5185\u5b58\u5360\u7528\u3001\u89c6\u9891\u7f16\u7801\u53c2\u6570\u7b49\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS\u5f55\u5c4f\u5f00\u53d1\u662f\u6307\u5728iOS\u8bbe\u5907\u4e0a\u5b9e\u73b0\u5f55\u5236\u5c4f\u5e55\u89c6\u9891\u7684\u529f\u80fd\u3002\u5728iOS 11\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u82f9\u679c\u516c\u53f8\u5df2\u7ecf\u539f\u751f\u652f\u6301\u4e86\u5c4f\u5e55\u5f55\u5236\u529f\u80fd\uff0c\u4e0d\u8fc7\u5728\u4e00\u4e9b\u5b9a\u5236\u5316\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u81ea\u5df1\u5f00\u53d1\u5f55\u5c4f\u529f\u80fd\u3002\u4e00\u3001\u539f\u7406iOS\u5f55\u5c4f\u7684<\/p>\n","protected":false},"author":25,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1969,1967,690,42,1968],"topic":[],"class_list":["post-849","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-apk","tag-app","tag-690","tag-42","tag-1968"],"_links":{"self":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/comments?post=849"}],"version-history":[{"count":0,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/849\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/media?parent=849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/categories?post=849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/tags?post=849"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/topic?post=849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}