发布Core Foundation对象

时间:2013-11-19 11:16:30

标签: ios iphone core-foundation

嘿,我使用此方法返回NSData。

-(NSData*)getPersonPicture:(NSDictionary *)person {
    NSData *imageData = nil;
    if (![person valueForKey:FIELD_PERSON_IMAGEDATA]) {
        return imageData;
    }
    if ([[[person valueForKey:FIELD_PERSON_IMAGEDATA] description]containSubString:@"http"]) {
        return [NSData dataWithContentsOfURL:[NSURL URLWithString:[person valueForKey:FIELD_PERSON_IMAGEDATA]]];
    } else {
        ABRecordRef _person = ABAddressBookGetPersonWithRecordID(_aBook,[[person valueForKey:FIELD_PERSON_IMAGEDATA] integerValue]);
        imageData = (__bridge NSData*)ABPersonCopyImageDataWithFormat(_person, kABPersonImageFormatThumbnail);
        return imageData;
    }
}

我无法确定何时需要发布此imageData。我不能这样离开吧?

1 个答案:

答案 0 :(得分:3)

如果您使用ARC,则ARC需要获得Core Foundation对象的所有权 - 这意味着ARC将对其负责。您可以使用宏CFBridgingRelease完成此操作:

    imageData = CFBridgingRelease(ABPersonCopyImageDataWithFormat(_person, kABPersonImageFormatThumbnail));
    return imageData;

使用非ARC:

(注意:通常,我们应该利用ARC!)

-(NSData*)getPersonPicture:(NSDictionary *)person {
    NSData *imageData = nil;
    if (![person valueForKey:FIELD_PERSON_IMAGEDATA]) {
        return imageData;
    }
    if ([[[person valueForKey:FIELD_PERSON_IMAGEDATA] description]containSubString:@"http"]) {
        return [NSData dataWithContentsOfURL:[NSURL URLWithString:[person valueForKey:FIELD_PERSON_IMAGEDATA]]];
    } else {
        ABRecordRef _person = ABAddressBookGetPersonWithRecordID(_aBook,[[person valueForKey:FIELD_PERSON_IMAGEDATA] integerValue]);
        CFDataRef cfData = ABPersonCopyImageDataWithFormat(_person, kABPersonImageFormatThumbnail);
        imageData = [[(NSData*)cfData retain] autorelease];
        return imageData;
    }
}