我正在运行此代码以创建包含呼叫者姓名的图像,并将其设置为特定联系人,但是在运行时,我收到 Received memory 警告并且它崩溃了...
-(void)RunActionInBlack{
//black bg - white text
ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
CFArrayRef array=ABAddressBookCopyArrayOfAllPeople(addressBook);
ABRecordRef person;
int len=CFArrayGetCount(array);
for (int i = 0; i<len; i++){
person = CFArrayGetValueAtIndex(array, i);
details.text = [NSString stringWithFormat:@"Done. %d of %d contacts", i+1,len];
[act stopAnimating];
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (firstName == NULL) {
firstName = @"";
}
if (lastName == NULL ) {
lastName = @"";
}
UIImage *im = [self addText:[UIImage imageNamed:@"black.png"] andText:[NSString stringWithFormat:@"%@ %@",firstName, lastName]];
NSData *dataRef = UIImagePNGRepresentation(im);
ABPersonSetImageData(person, (CFDataRef)dataRef, &error);
[lastName release];
[firstName release];
[dataRef release];
CFRelease(dataRef);
[im release];
}
ABAddressBookSave(addressBook, &error);
CFRelease(array);
CFRelease(addressBook);
}
创建文本:
-(UIImage *)addText:(UIImage *)img andText:(NSString*)txt{
UIImageView *imView =[[UIImageView alloc] initWithImage:img ];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[tempView addSubview:imView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, img.size.width, img.size.height)];
[label setText:txt];
[label setFont:[UIFont boldSystemFontOfSize:160]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:10];
[tempView addSubview:label];
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[label release];
[tempView release];
[imView release];
return finalImage;
}
我没有任何内存泄漏或什么的...
我正在运行这些方法,例如:[self performSelectorInBackground:@selector(RunActionInWhite) withObject:nil];
提前致谢。