2009-09-16 3 views
2

다른 누구도이 문제를 겪고 있습니까? 나는 NSTimer로 꽤 자주 이미지 크기를 조정하고있다. 인스트루먼트를 사용하면 메모리 누수가 표시되지 않지만 objectalloc은 계속 올라갑니다. CGBitmapContextCreateImage를 직접 가리 킵니다.iPhone - CGBitmapContextCreateImage 누출,이 문제가있는 사용자는 누구입니까?

누구나 해결책을 알고 계십니까? 또는 심지어 가능한 아이디어? 그냥 전성 검사

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality 
{ 
    CGImageRef   imageRef = [inImage CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

    // Build a bitmap context that's the size of the thumbRect 
    CGContextRef bitmap = CGBitmapContextCreate(
        NULL, 
        thumbRect.size.width, 
        thumbRect.size.height,  
        CGImageGetBitsPerComponent(imageRef), 
        4 * thumbRect.size.width, 
        CGImageGetColorSpace(imageRef), 
        alphaInfo 
        ); 

    // Draw into the context, this scales the image 
    CGContextSetInterpolationQuality(bitmap, interpolationQuality); 
    CGContextDrawImage(bitmap, thumbRect, imageRef); 

    // Get an image from the context and a UIImage 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* result = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); // ok if NULL 
    CGImageRelease(ref); 

    return [result autorelease]; 
} 

답변

0

: 당신은 반환있는 UIImage를 해제하는 - 일반적으로 내가 이름으로 만들도록하는 (이 경우있는 UIImage에) 새로운 객체를 할당하는 기능을 기대?

은 아마 당신은

return [result autorelease] 

줄까?

+0

나는 실제로 그 코드를 몇 분 전에 코드에 추가했다. CGBitmapContextCreate가 내 objectalloc을 살인하고 있습니다. – bbullis21

0

더 간단하게 사용하지 않는 이유는 무엇입니까 UIGraphicsBeginImageContext?

@implementation UIImage(ResizeExtension) 
- (UIImage *)resizedImageWithSize:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)interpolationQuality; 
@end 
@implementation UIImage(ResizeExtension) 
- (UIImage *)resizedImageWithSize:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)interpolationQuality 
{ 
    UIGraphicsBeginImageContext(newSize); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetInterpolationQuality(context, interpolationQuality); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return result; 
} 

이 현재 오토 릴리즈 풀 유지 화상을 반환, 또한

을 @end; 루프에서 이러한 이미지를 많이 만들려면 NSAutoreleasePool을 수동으로 할당하고 배수하십시오.

+0

UIGraphicsBeginImageContext는 스레드로부터 안전하지 않습니다. –

+0

UIImage는 기술적으로 스레드로부터 안전하지 않습니다. (보통 작동하지만) – rpetrich

+0

팁 주셔서 감사하지만 drawInRect는 동일한 InterpolationQuality 영향을 제공하지 않습니다. 이것은 기본적으로 위의 resizeImage 스크립트를 호출하여 이미지를 높은 InterpolationQuality로 작은 해상도로 축소 한 다음 정상으로 다시 크기를 조정합니다. 최종 결과는 Gaussian 흐린 이미지와 같은 Photoshop입니다.CGBitmapContextCreate가 왜 objectalloc을 구성하는지 이해할 수있는 날을 찾고있었습니다. 팁 주셔서 감사합니다. 나는 똑같은 최종 결과를 얻을 수 있는지 확인하기 위해 귀하의 미리보기를 계속 사용할 것입니다. 가능한 다른 방법을 아십니까? – bbullis21

1

imageRef를 출시해야합니까?

CGImageRelease(imageRef); 
0

좋아, 여기에 문제가 우리가 CGImageRef로 CGBitmapContextCreateImage의 반환을 정의하는 것입니다, 그것의 CGImage해야합니다. 할당량 (malloc을 가정 할 때)이 끊임없이 증가하는 이유는 CGImage 자체가 결코 공개되지 않기 때문입니다. 아래 코드를 사용해보십시오. 또한 'Alloc'd가 아니므로 결과를 자동으로 올릴 필요가 없습니다.

할당을 다시 사용하여 변경 사항을 실행 한 후에는 이번에는 라이브 바이트가 계속 증가하지 않을 것입니다.

PC에 입력 했으므로 XCode에 놓으면 구문 오류가 발생할 수 있습니다. 그러나 이것은 트릭을해야합니다.

// Get an image from the context and a UIImage  
CGImage cgImage = CGBitmapContextCreateImage(bitmap);  
UIImage* result = [UIImage imageWithCGImage:cgImage];  
CGContextRelease(bitmap); // ok if NULL  
CGImageRelease(cgImage);  
return result; 
+0

CGBitmapContextCreateImage는 CGImage가 아닌 CGImageRef를 반환합니다. CGImageRef는 CGImage *의 typedef입니다. 위의 코드는 컴파일되지 않으며 CGBitmapContextCreateImage가 CGImageRef (CGImage *)를 반환하도록 수정 된 경우 CGImageRef를 해제합니다. 대답이 잘못되었습니다. 제거하는 것을 고려해야합니다. –

0

가비지 수집을 사용하는 경우 CFMakeCollectable (posterFrame)을 사용하십시오. 당신은 기존의 메모리 관리를 사용하는 경우, 그것은 매우 간단합니다 :

return (CGImageRef)[(id)posterFrame autorelease]; 

당신은 목표 - C 객체 포인터 (이 경우하는 CGImageRef 단위) CFTypeRef 캐스팅, 그것에게 -autorelease 메시지를 전송 한 다음 캐스트 CGImageRef의 결과. 이 패턴은 (거의) CFRetain() 및 CFRelease()와 호환되는 모든 유형에서 작동합니다.