Showing posts with label Profile pic downloading.... Show all posts
Showing posts with label Profile pic downloading.... Show all posts

24 Jun 2017

Getting Profile image from service...


Getting Profile image from service...



-(void)viewWillAppear:(BOOL)animated {

      NSUserDefaults *loginUserData = [NSUserDefaults standardUserDefaults];
    
    NSString *imageUrl = [NSString stringWithFormat:@"Your URL/%@",your previously uploaded img id or getting from login];
    
    [self setupImage:imageUrl];
    
   
    
}

-(void)setupImage:(NSString *)imageUrl
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"%@", imageUrl]]) {
        self.profilePicImageView.image =[UIImage imageWithData:[[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"%@", imageUrl]]];
        
    }else{
        [self downloadImageWithURL:[NSURL URLWithString:imageUrl] completionBlock:^(BOOL completed, UIImage *image, NSError *error, NSData *data){
            if (completed) {
                
                self.profilePicImageView.image = image;
                [[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:@"%@", imageUrl]];
            }
        }];
    }
}

- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image, NSError *error, NSData *data))completionBlock
{

    NSUserDefaults *authToken= [NSUserDefaults standardUserDefaults];
    NSString *authStr=[authToken valueForKey:@"authToken"];
  
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:authStr forHTTPHeaderField:@"X-Authorization"];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if ( !error )
                               {
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   completionBlock(YES, image, error, data);
                               } else{
                                   completionBlock(NO, nil, error, data);
                               }
                           }];
}


Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0