iphone - 如果我从 (__bridge) 使用 CGImageSourceCreateWithData,是否需要 "CFRelease"?

标签 iphone objective-c ios core-foundation

我的问题很简单,我想知道 "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/

相关文章:

iphone - 无法读取 Xcode 4.2 中的符号错误

ios - VC之间传输数据

未收到 iOS 推送通知

iOS Storyboard 约束未按预期运行

iphone - 同级 View 之间的 NSNotification

iphone - UITextView 和 UIPickerView 都有自己的 UIToolbar

ios - 应用程序适用于模拟器,但不适用于设备。奇怪的错误信息? X代码

ios - 我可以在两个 UITableViewCell 之间构建一个 UITableView 吗?

iphone - 我的二维码扫描器无法扫描包含特殊字符的特定 URL

objective-c - 编译器会为类别中声明的属性自动合成一个 ivar 吗?