2010년 4월 12일 월요일

Mac objectC 이미지 저장 방법

command line Tool 로 이미지 캡쳐기 만들던 중
결론을 내려준 코드 ... 2박3일 삽질한 원인 두가지

1. __NSAutoreleaseNoPool(): Object 0x100306530 of class NSCFString autoreleased with no pool in place - just leaking
해결 : NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];
2. CGDataProviderRelease(dataProvider);

-
(void)saveJPEGImage:(CGImageRef)imageRef path:(NSString *)path {
CFMutableDictionaryRef mSaveMetaAndOpts = CFDictionaryCreateMutable(nil, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(mSaveMetaAndOpts, kCGImageDestinationLossyCompressionQuality,
[NSNumber numberWithFloat:1.0]); // set the compression quality here
NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path];
CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.jpeg" , 1, NULL);
CGImageDestinationAddImage(dr, imageRef, mSaveMetaAndOpts);
CGImageDestinationFinalize(dr);
}


-(void)savePNGImage:(CGImageRef)imageRef path:(NSString *)path {
NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path];
CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.png" , 1, NULL);
CGImageDestinationAddImage(dr, imageRef, NULL);
CGImageDestinationFinalize(dr);
}

-(void)saveTIFFImage:(CGImageRef)imageRef path:(NSString *)path {
int compression = NSTIFFCompressionLZW; // non-lossy LZW compression
CFMutableDictionaryRef mSaveMetaAndOpts = CFDictionaryCreateMutable(nil, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef tiffProfsMut = CFDictionaryCreateMutable(nil, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(tiffProfsMut, kCGImagePropertyTIFFCompression, CFNumberCreate(NULL, kCFNumberIntType, &compression));
CFDictionarySetValue(mSaveMetaAndOpts, kCGImagePropertyTIFFDictionary, tiffProfsMut);

NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path];
CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.tiff" , 1, NULL);
CGImageDestinationAddImage(dr, imageRef, mSaveMetaAndOpts);
CGImageDestinationFinalize(dr);
}

댓글 없음:

댓글 쓰기