我收到内存错误。由于内存使用量呈指数增长,因此出现内存错误。很明显,我没有发布什么,有什么想法可能是什么?访问ALAssetsLibrary时出现内存错误
这是我的方法来确定在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.
我认为在一个循环中调用'fullResolutionImage'并不是一个好主意。这是在代码中创建内存问题的非常好的候选人。无论如何,你可以在你的仪器中分析你的代码,并确切地看到那里发生了什么。 – faviomob