现在的位置: 首页 > 综合 > 正文

cocoa quartz保存image的一种方式 带image格式等选项

2013年06月14日 ⁄ 综合 ⁄ 共 3110字 ⁄ 字号 评论关闭

- (void) saveImage:(CGContextRef) bitmap path:(NSString *)strpath type:(int)formattype
dpi:(int)ndpi

{

    CGImageRef imageRef =
CGBitmapContextCreateImage(bitmap);

   
if (imageRef == NULL) {

       
CGImageRelease(imageRef);

        //CGContextRelease(bitmap);

       
return ;

    }

    

NSString *finalPath = [NSString
stringWithString:strpath];

CFURLRef url =
CFURLCreateWithFileSystemPath (

  kCFAllocatorDefault,

  (CFStringRef)finalPath,

  kCFURLPOSIXPathStyle,

 
false);

CGImageDestinationRef dest =
NULL;

if (formattype ==
0) {

dest = CGImageDestinationCreateWithURL(url,
CFSTR("public.jpeg"),
1, NULL);

assert(dest);

CFMutableDictionaryRef  mSaveMetaAndOpts =
CFDictionaryCreateMutable(nil,
0,

                                                                             &kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);

        CFDictionarySetValue(mSaveMetaAndOpts,
kCGImageDestinationLossyCompressionQuality

[NSNumber
numberWithFloat:0.8]);

       
CFDictionarySetValue(mSaveMetaAndOpts,
kCGImagePropertyDPIHeight

[NSNumber
numberWithInteger:ndpi]);

       
CFDictionarySetValue(mSaveMetaAndOpts,
kCGImagePropertyDPIWidth

[NSNumber
numberWithInteger:ndpi]);

        

CGImageDestinationAddImage(dest, imageRef, (CFDictionaryRef)mSaveMetaAndOpts);

CFRelease(mSaveMetaAndOpts);

//CGImageDestinationAddImage(dest, imageRef, NULL);

assert(dest);

if (dest ==
NULL) {

NSLog(@"CGImageDestinationCreateWithURL failed");

}

//NSLog(@"%@", dest);

assert(CGImageDestinationFinalize(dest));

//这三句话用来释放对象

CFRelease(dest);

//CGImageRelease(imageRef);

//CFRetain(url);

CFRelease(url);

}

else
if (formattype ==
1
)

{

dest = CGImageDestinationCreateWithURL(url,
CFSTR("public.tiff"),
1, NULL);

assert(dest);

        

       
CFMutableDictionaryRef  mSaveMetaAndOpts =
CFDictionaryCreateMutable
(nil,
0
,

                                                                             &kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);

       
CFDictionarySetValue(mSaveMetaAndOpts,
kCGImagePropertyDPIHeight

[NSNumber
numberWithInteger:ndpi]);

       
CFDictionarySetValue(mSaveMetaAndOpts,
kCGImagePropertyDPIWidth

[NSNumber
numberWithInteger:ndpi]);

CGImageDestinationAddImage(dest, imageRef, (CFDictionaryRef)mSaveMetaAndOpts);

assert(dest);

if (dest ==
NULL) {

NSLog(@"CGImageDestinationCreateWithURL failed");

}

//NSLog(@"%@", dest);

assert(CGImageDestinationFinalize(dest));

//这三句话用来释放对象

CFRelease(dest);

//CGImageRelease(imageRef);

//CFRetain(url);

CFRelease(url);

}

else {

dest = CGImageDestinationCreateWithURL(url,
CFSTR("public.png"),
1, NULL);

assert(dest);

        

       
CFMutableDictionaryRef  mSaveMetaAndOpts =
CFDictionaryCreateMutable
(nil,
0
,

                                                                             &kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);

       
CFDictionarySetValue(mSaveMetaAndOpts,
kCGImagePropertyDPIHeight

[NSNumber
numberWithInteger:ndpi]);

       
CFDictionarySetValue(mSaveMetaAndOpts,
kCGImagePropertyDPIWidth

[NSNumber
numberWithInteger:ndpi]);

CGImageDestinationAddImage(dest, imageRef, (CFDictionaryRef)mSaveMetaAndOpts);

CGImageDestinationAddImage(dest, imageRef,
NULL);

assert(dest);

if (dest ==
NULL) {

NSLog(@"CGImageDestinationCreateWithURL failed");

}

//NSLog(@"%@", dest);

assert(CGImageDestinationFinalize(dest));

//这三句话用来释放对象

CFRelease(dest);

//CGImageRelease(imageRef);

//CFRetain(url);

CFRelease(url);

}

    

   
CGImageRelease(imageRef);

CGContextFlush(bitmap);

}

抱歉!评论已关闭.