Ответы пользователя по тегу Objective-C
  • Как вытащить фотки из Instagram и заполнить ими CollectionView cell?

    @ermolushka Автор вопроса
    В общем проблема решена. Я создал класс CollectionViewCell и прописал примерно следующее:

    @implementation PhotosCollectionViewController
    
    static NSString * const reuseIdentifier = @"Cell";
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        self.accessToken = [userDefaults objectForKey:@"accessToken"];
        
        if (self.accessToken == nil) {
            
            SimpleAuth.configuration[@"instagram"] = @{
                                                       @"client_id" : @"XXX",
                                                       SimpleAuthRedirectURIKey : @"XXX"
                                                       };
            
            
            [SimpleAuth authorize:@"instagram" completion:^(NSDictionary *responseObject, NSError *error) {
                
                NSString *accessToken = responseObject[@"credentials"][@"token"];
                [userDefaults setObject:accessToken forKey:@"accessToken"];
                [userDefaults synchronize];
            }];
        } else{
            
            NSString *string = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/users/{user-id}/media/recent?access_token=%@",self.accessToken];
            NSURL *url = [NSURL URLWithString:string];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            operation.responseSerializer = [AFJSONResponseSerializer serializer];
            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                
                //self.photos = [responseObject objectForKey:@"data"];
                
                self.photos = [[[[responseObject valueForKey:@"data"]valueForKey:@"images"]valueForKey:@"low_resolution"]valueForKey:@"url"];
                
                
                [self.collectionView reloadData];
                //NSLog(@"%@",responseObject);
                NSLog(@"%@", self.photos);
                
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Request Failed: %@, %@", error, error.userInfo);    }
             ];
            
            [operation start];
            
        }
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
     #pragma mark - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     // Get the new view controller using [segue destinationViewController].
     // Pass the selected object to the new view controller.
     }
     */
    
    #pragma mark <UICollectionViewDataSource>
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    }
    
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return self.photos.count;
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        
        // Configure the cell
       
    
        NSString *url = [self.photos objectAtIndex:indexPath.row];
        cell.image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
        return cell;
        
        
    }
    Ответ написан
    Комментировать