Sosily
Sosily

Reputation: 726

Release Core Foundation object

Hey I use this method to return 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;
    }
}

I can't figure when I need to release this imageData. I can't leave it like this , right?

Upvotes: 1

Views: 135

Answers (1)

CouchDeveloper
CouchDeveloper

Reputation: 19116

If you are using ARC, then ARC need to take ownership of the Core Foundation object - which means, ARC will become responsible for it. You can accomplish this with macro CFBridgingRelease:

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

Using non-ARC:

(note: usually, we should leveraging 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;
    }
}

Upvotes: 3

Related Questions