Created
December 17, 2015 12:07
-
-
Save mutekinootoko/b11d333d3503d082eee5 to your computer and use it in GitHub Desktop.
put avatar on button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setup code to draw / display avatar | |
UIView *avatarView = [[UIView alloc] init]; | |
avatarView.frame = CGRectMake(20, 50, 280, 100); | |
avatarView.layer.borderColor = [UIColor redColor].CGColor; | |
avatarView.layer.borderWidth = 3.0f; | |
[self.view addSubview:avatarView]; | |
// do additional loading for avatars | |
UIButton *avatarButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | |
// the last two values control the size of the button | |
avatarButton.frame = CGRectMake(0, 0, 80, 80); | |
// make corners round | |
avatarButton.layer.cornerRadius = 35; // value varies -- // 35 yields a pretty good circle. | |
avatarButton.clipsToBounds = YES; | |
// retrieve image from Core Data and place on UIButton | |
NSFetchRequest *request = [[NSFetchRequest alloc] init]; | |
// define table / entity to use | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account"inManagedObjectContext:_managedObjectContext]; | |
[request setEntity:entity]; | |
// fetch records and handle error | |
NSError *error; | |
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; | |
if (!mutableFetchResults) { | |
// handle error | |
} | |
for (Account *anAccount in mutableFetchResults) { | |
if ([anAccount.username isEqualToString:lblUSERNAME.text]) { | |
NSLog(@"username found."); | |
// fetch avatar / attribute based on username | |
_avatar = [UIImage imageWithData:anAccount.avatar scale:1]; | |
} | |
} | |
if (_avatar == nil) { | |
NSLog(@"couldn't find avatar"); | |
} else { | |
[avatarButton setBackgroundImage:_avatar forState:UIControlStateNormal]; | |
} | |
// add button to subview | |
[avatarView addSubview:avatarButton]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment