Skip to content

Instantly share code, notes, and snippets.

@wimbledon
Created April 21, 2014 16:42
Show Gist options
  • Save wimbledon/11148297 to your computer and use it in GitHub Desktop.
Save wimbledon/11148297 to your computer and use it in GitHub Desktop.
IMProfileImageView
//
// IMProfileImageView.m
// Imaggle
//
// Created by David Liu on 3/14/14.
// Copyright (c) 2014 Imaggle. All rights reserved.
//
#import "IMProfileImageView.h"
#import "UIImageView+AFNetworking.h"
#import "FBPictureHelper.h"
@interface IMProfileImageView()
@property (nonatomic, strong) NSString *facebookId;
@end
@implementation IMProfileImageView
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
self.backgroundColor = [UIColor clearColor];
self.image = [UIImage imageNamed:@"AvatarPlaceholder"];
self.contentMode = UIViewContentModeScaleAspectFill;
self.layer.cornerRadius = 5.0f;
self.layer.masksToBounds = YES;
UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTap:)];
[self addGestureRecognizer:tg];
}
- (void)didTap:(id)sender
{
[self.delegate profileImageViewSelected:self];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self loadImage];
}
- (void) setFacebookId:(NSString *)facebookId
{
if (![_facebookId isEqualToString: facebookId]) {
_facebookId = facebookId;
[self loadImage];
}
}
- (void) loadImage
{
if (self.facebookId.length) {
if (self.frame.size.width > 32.0 && self.facebookId.length) {
NSURL *u = [FBPictureHelper urlForProfilePictureWithWidthClosestTo:kIMContentScaleFactor * self.frame.size.width
ForFBID:self.facebookId];
[self setAppearingImageWithURL:u];
}
else {
[self setAppearingImageWithURL:[FBPictureHelper urlForProfilePictureSizeSquareForFBID: self.facebookId]];
}
}
else {
self.image = [UIImage imageNamed:@"AvatarPlaceholder"];
}
}
- (void)setAppearingImageWithURL:(NSURL *)url
{
__weak __typeof(self)weakSelf = self;
NSURLRequest *pr = [[NSURLRequest alloc]initWithURL:url];
[self setImageWithURLRequest:pr
placeholderImage:[UIImage imageNamed:@"AvatarPlaceholder"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (!request && !response) {
weakSelf.image = image;
}
else {
[UIView transitionWithView:weakSelf
duration:kIMAnimationProfilePicDuration
options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
weakSelf.image = image;
} completion:nil];
}
}
failure:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment