尝试通过资源库使用 NSMutableData 获取字节并附加视频时会出现内存已满错误

发布于 2024-09-25 01:33:40 字数 272 浏览 3 评论 0原文

我正在尝试通过资源库上传一个大小为 100MB 的视频。但是当我尝试使用 ALAssetRepresentation 的 -(NSUInteger)getBytes:(uint8_t *)buffer fromOffset:(long long)offset length:(NSUInteger)length error:(NSError **)error 我收到内存已满错误。我还需要将缓冲区中的数据放入 NSData 中。我怎样才能做到这一点?

I’m trying to upload a video of size 100MB through Asset Library. But when i try to use -(NSUInteger)getBytes:(uint8_t *)buffer fromOffset:(long long)offset length:(NSUInteger)length error:(NSError **)error of ALAssetRepresentation I get memory full error. I also need to put the data in buffer to NSData. How can i achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不…忘初心 2024-10-02 01:33:40

我尝试了这个方法:

Byte *buffer = (Byte*)malloc(asset.defaultRepresentation.size);

NSUInteger k = [asset.defaultRepresentation getBytes:buffer fromOffset: 0.0
                length:asset.defaultRepresentation.size error:nil];

NSData *adata = NSData *adata = [NSData dataWithBytesNoCopy:buffer
                                 length:j freeWhenDone:YES];    

确实有效!

I tried this way:

Byte *buffer = (Byte*)malloc(asset.defaultRepresentation.size);

NSUInteger k = [asset.defaultRepresentation getBytes:buffer fromOffset: 0.0
                length:asset.defaultRepresentation.size error:nil];

NSData *adata = NSData *adata = [NSData dataWithBytesNoCopy:buffer
                                 length:j freeWhenDone:YES];    

It really works!

御弟哥哥 2024-10-02 01:33:40

正如 @runeb 所说,答案不适用于大文件。您应该这样做:

            int bufferSize = 2048;
            int offset = 0;

            NSString* name=nil;
            while(offset<asset.size){
                Byte *buffer = (Byte*)malloc(bufferSize);
                NSUInteger buffered = [asset getBytes:buffer fromOffset:offset length:bufferSize error:nil];
                NSData *data;

                data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:NO];

                if(!name){
                    //Creates the file and gives it a unique name
                    name = [FileUtils saveVideoFromAsset:data];
                }
                else{
                    //Append data to the file created...
                    [FileUtils appendData:data toFile:name];
                }
                offset+=buffered;
               free(buffer);
            }

为了将数据附加到文件中,您可以使用:

NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    [myHandle seekToEndOfFile];
    [myHandle writeData:videoData];

我希望有帮助!

As @runeb said the answer is not working properly with large files. You should do something like that:

            int bufferSize = 2048;
            int offset = 0;

            NSString* name=nil;
            while(offset<asset.size){
                Byte *buffer = (Byte*)malloc(bufferSize);
                NSUInteger buffered = [asset getBytes:buffer fromOffset:offset length:bufferSize error:nil];
                NSData *data;

                data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:NO];

                if(!name){
                    //Creates the file and gives it a unique name
                    name = [FileUtils saveVideoFromAsset:data];
                }
                else{
                    //Append data to the file created...
                    [FileUtils appendData:data toFile:name];
                }
                offset+=buffered;
               free(buffer);
            }

In order to append data to a file you can use that:

NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    [myHandle seekToEndOfFile];
    [myHandle writeData:videoData];

I hope that helps!

埖埖迣鎅 2024-10-02 01:33:40

我们需要添加 @autoreleasepool 块,以便清理任何自动释放的对象。 iOS7之后ARC好像发生了一些变化

@autoreleasepool {
     NSUInteger readStatus = [rep getBytes:buffer fromOffset:_startFromByte length:chunkSize error:NULL];
}

ust add @autoreleasepool block, so that any autorleased objects should be cleaned up. it looks like that ARC has something changed after iOS7

@autoreleasepool {
     NSUInteger readStatus = [rep getBytes:buffer fromOffset:_startFromByte length:chunkSize error:NULL];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文