gpt4 book ai didi

ios - 访问 ALAssetsLibrary 时出现内存错误

转载 作者:行者123 更新时间:2023-11-29 03:12:45 25 4
gpt4 key购买 nike

我遇到内存错误。我收到内存错误,因为内存使用量呈指数增长。显然我不会发布任何东西,你知道那可能是什么吗?

这是我确定 UI 图像中存在的红色像素的方法。它返回计数。

- (NSUInteger)getRedPixelCount:(UIImage*)image
{
NSUInteger numberOfRedPixels = 0;

struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
if (pixels != nil)
{
CGContextRef context = CGBitmapContextCreate((void *) pixels,
image.size.width,
image.size.height,
8,
image.size.width * 4,
CGImageGetColorSpace(image.CGImage),
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);

if (context != NULL)
{
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);
NSUInteger numberOfPixels = image.size.width * image.size.height;

while (numberOfPixels > 0) {
if (pixels->r == 255) {
numberOfRedPixels++;

}
pixels++;
numberOfPixels--;
}
CGContextRelease(context);
}
}
return numberOfRedPixels;

}

这是迭代照片库图像并确定每个红色像素的代码。

[self.library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){

if (asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];

if (iref){

UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];

NSLog(@"%i", [self getRedPixelCount:myImage]);
}
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];

问候,C.

最佳答案

你没有释放由

分配的内存
struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));

你需要添加:

free(pixels);

在 if(pixels != nil) block 的底部。

让第一个 block 看起来像:

- (NSUInteger)getRedPixelCount:(UIImage*)image
{
NSUInteger numberOfRedPixels = 0;

struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
if (pixels != nil)
{
CGContextRef context = CGBitmapContextCreate((void *) pixels,
image.size.width,
image.size.height,
8,
image.size.width * 4,
CGImageGetColorSpace(image.CGImage),
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);

if (context != NULL)
{
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);
NSUInteger numberOfPixels = image.size.width * image.size.height;
struct pixels* ptr = pixels;

while (numberOfPixels > 0) {
if (ptr->r == 255) {
numberOfRedPixels++;

}
ptr++;
numberOfPixels--;
}
CGContextRelease(context);
}
free(pixels);
}
return numberOfRedPixels;
}

如果第二个 block 更改为包括:

        @autoreleasepool {
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];

if (iref){

UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];

NSLog(@"%i", [self getRedPixelCount:myImage]);
<#statements#>
}
}

虽然主要泄漏不是释放像素缓冲区。

关于ios - 访问 ALAssetsLibrary 时出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049566/

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