Skip to content

Commit dfe5387

Browse files
author
zhaoyongqiang
committed
fix yuv转image buffer没有释放的问题
1 parent 3abc0df commit dfe5387

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

iOS/APIExample/Common/Utils/MediaUtils.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ + (UIImage *)i420ToImage:(void *)srcY srcU:(void *)srcU srcV:(void *)srcV width:
2424
unsigned char * NV12buf = (unsigned char *)malloc(size);
2525
[self yuv420p_to_nv12:buf nv12:NV12buf width:width height:height];
2626

27+
free(buf);
28+
2729
int w = width;
2830
int h = height;
2931
NSDictionary *pixelAttributes = @{(NSString*)kCVPixelBufferIOSurfacePropertiesKey:@{}};
@@ -36,6 +38,7 @@ + (UIImage *)i420ToImage:(void *)srcY srcU:(void *)srcU srcV:(void *)srcV width:
3638
&pixelBuffer);
3739
if (result != kCVReturnSuccess) {
3840
NSLog(@"Unable to create cvpixelbuffer %d", result);
41+
free(NV12buf);
3942
return nil;
4043
}
4144

@@ -58,11 +61,11 @@ + (UIImage *)i420ToImage:(void *)srcY srcU:(void *)srcU srcV:(void *)srcV width:
5861
CGImageRef videoImage = [temporaryContext createCGImage:coreImage
5962
fromRect:CGRectMake(0, 0, w, h)];
6063

61-
UIImage *finalImage = [[UIImage alloc] initWithCGImage:videoImage
62-
scale:1.0
63-
orientation:UIImageOrientationRight];
64+
UIImage *finalImage = [[UIImage alloc] initWithCGImage:videoImage];
6465
CVPixelBufferRelease(pixelBuffer);
6566
CGImageRelease(videoImage);
67+
68+
free(NV12buf);
6669
return finalImage;
6770
}
6871

macOS/APIExample/Commons/Utils/MediaUtils.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ + (NSImage *)i420ToImage:(void *)srcY srcU:(void *)srcU srcV:(void *)srcV width:
2525
unsigned char * NV12buf = (unsigned char *)malloc(size);
2626
[self yuv420p_to_nv12:buf nv12:NV12buf width:width height:height];
2727

28+
free(buf);
29+
2830
int w = width;
2931
int h = height;
3032
NSDictionary *pixelAttributes = @{(NSString*)kCVPixelBufferIOSurfacePropertiesKey:@{}};
@@ -37,6 +39,7 @@ + (NSImage *)i420ToImage:(void *)srcY srcU:(void *)srcU srcV:(void *)srcV width:
3739
&pixelBuffer);
3840
if (result != kCVReturnSuccess) {
3941
NSLog(@"Unable to create cvpixelbuffer %d", result);
42+
free(NV12buf);
4043
return nil;
4144
}
4245

@@ -62,6 +65,8 @@ + (NSImage *)i420ToImage:(void *)srcY srcU:(void *)srcU srcV:(void *)srcV width:
6265
NSImage *finalImage = [[NSImage alloc] initWithCGImage:videoImage size:NSMakeSize(width, height)];
6366
CVPixelBufferRelease(pixelBuffer);
6467
CGImageRelease(videoImage);
68+
69+
free(NV12buf);
6570
return finalImage;
6671
}
6772

0 commit comments

Comments
 (0)