我的问题很简单,我想知道 "CGImageSourceCreateWithData" 方法是否创建了一个复制我提供的数据的新对象,以便我在不需要时必须释放它不再使用它,或者如果它只是创建对我已经拥有的数据的引用,那么如果我释放它,我将丢失这些数据(并且可能出现错误的访问错误)。
此问题与使用 (__bridge CFDataRef) 作为源数据有关。这让我可以在 ARC 模式下免费使用 Core Foundation 对象。
考虑以下函数(或方法,不确定它是如何调用的):
- (void)saveImageWithData:(NSData*)jpeg andDictionary:(NSDictionary*)dicRef andName:(NSString*)name
{
[self setCapturedImageName:name];
CGImageSourceRef source ;
// Notice here how I use __bridge
source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL);
CFStringRef UTI = CGImageSourceGetType(source);
NSMutableData *dest_data = [NSMutableData data];
// And here I use it again
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);
CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) dicRef);
BOOL success = NO;
success = CGImageDestinationFinalize(destination);
if(!success) {
NSLog(@"***Could not create data from image destination ***");
}
// This only saves to the disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"ARPictures"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", name]]; //add our image to the path
[dest_data writeToFile:fullPath atomically:YES];
self.img = [[UIImage alloc] initWithData:dest_data];
self.capturedImageData = [[NSData alloc] initWithData:dest_data];
//This is what im not sure if i should use
CFRelease(destination);
CFRelease(source);
}
我担心的是内存泄漏或我不应该取消分配的东西。
谢谢
最佳答案
你做得对。这些例程是否制作副本实际上并不重要。重要的是您 CFRelease
您“创建”(或“复制”)的内容。这里的一切看起来都是正确的。 __bridge
在传递参数时是合适的,因为您实际上并没有将对象从 CF 传输到 Cocoa,反之亦然。您只是暂时“桥接”(转换)它。
关于iphone - 如果我从 (__bridge) 使用 CGImageSourceCreateWithData,是否需要 "CFRelease"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475499/