Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- {
- return 1;
- }
- -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return [[self objects] count];
- }
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
- {
- NSArray *people = [self objects];
- static NSString *CellIdentifier = @"Cell";
- VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
- PFObject *current;
- current = [people objectAtIndex:indexPath.item];
- PFFile *userImageFile = current[@"image"];
- [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
- UIImage *image = [UIImage imageWithData:imageData];
- [[cell contentView] setContentMode: UIViewContentModeScaleAspectFit];
- [[cell imageView] setImage:image];
- }];
- [[cell title] setText:[current valueForKey:@"title"]];
- [[cell price] setText:[NSString stringWithFormat: @"£%@", [current valueForKey:@"price"]]];
- return cell;
- }
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
- {
- NSArray *people = [self objects];
- static NSString *CellIdentifier = @"Cell";
- VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
- PFObject *current;
- current = [people objectAtIndex:indexPath.item];
- PFFile *userImageFile = current[@"image"];
- [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
- if (!error)
- {
- if (!image) {
- [[cell imageView] setImage:[UIImage imageNamed:@"placeholder.png"]];
- } else {
- image = [UIImage imageWithData:imageData];
- //resize image
- CGSize destinationSize = CGSizeMake(158,187);
- UIGraphicsBeginImageContext(destinationSize);
- [image drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
- //New image
- UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- //Optimise image
- NSData *imageDataCompressed = UIImageJPEGRepresentation(newImage, 0.4f);
- // NSLog(@"Image Size %@", NSStringFromCGSize(newImage.size));//log size of image
- NSLog(@"%@", [current valueForKey:@"title"]);
- [[cell imageView] setImage:[UIImage imageWithData:imageDataCompressed]];
- }
- }
- }];
- [[cell title] setText:[current valueForKey:@"title"]];
- [[cell price] setText:[NSString stringWithFormat: @"£%@", [current valueForKey:@"price"]]];
- return cell;
- }
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
- {
- NSArray *people = [self objects];
- static NSString *CellIdentifier = @"Cell";
- VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
- [cell.activityIndicator startAnimating];
- PFObject *current;
- current = [people objectAtIndex:indexPath.item];
- PFFile *userImageFile = current[@"image"];
- NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:userImageFile.url, indexPath.item]];
- NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataDontLoad
- timeoutInterval:6.0];
- [cell.imageView setImageWithURLRequest:urlRequest
- placeholderImage:[UIImage imageNamed:@"placeholder.png"]
- success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
- //resize image
- CGSize destinationSize = CGSizeMake(158,187);
- UIGraphicsBeginImageContext(destinationSize);
- [image drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
- //New image
- UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- //Optimise image
- NSData *imageDataCompressed = UIImageJPEGRepresentation(newImage, 0.4f);
- cell.imageView.image = [UIImage imageWithData:imageDataCompressed];
- NSLog(@"Image Size %@", NSStringFromCGSize(newImage.size));//log size of image
- [cell.activityIndicator stopAnimating];
- } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
- NSLog(@"Failed to download image: %@", error);
- }];
- return cell;
- }
- - (void)grabDataFromCloud
- {
- self.model = [NSMutableArray array];
- for (PFObject *object in [self objects]) {
- PFFile *imageFile = [object valueForKey:@"image"];
- NSURL *url = [NSURL URLWithString:imageFile.url];
- NSMutableDictionary *newObject = [NSMutableDictionary dictionaryWithDictionary:@{@"title": [object valueForKey:@"title"], @"price": [object valueForKey:@"price"], @"imageUrl": url}];
- [[self model] addObject:newObject];
- }
- }
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
- {
- [self grabDataFromCloud];
- static NSString *CellIdentifier = @"Cell";
- VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
- [cell.activityIndicator setHidden:YES];
- NSMutableDictionary* d = [self.model objectAtIndex:indexPath.item];
- cell.title.text = d[@"title"];
- cell.price.text = [NSString stringWithFormat:@"£%@", d[@"price"]];
- if (d[@"image"]) {
- cell.imageView.image = d[@"image"];
- } else { // if not, download it
- cell.imageView.image = nil;
- dispatch_queue_t backgroundQueue = dispatch_queue_create("test", 0);
- dispatch_async(backgroundQueue, ^{
- NSData* data = [NSData dataWithContentsOfURL:d[@"imageUrl"]];
- UIImage* img = [UIImage imageWithData:data];
- d[@"image"] = img;
- dispatch_async(dispatch_get_main_queue(), ^{
- //causes crash Assertion failure in -[UICollectionView _endItemAnimations],
- // /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3687
- // [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
- });
- });
- }
- return cell;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView
- cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- NSMutableDictionary* d = (self.model)[indexPath.row];
- cell.textLabel.text = d[@"text"];
- if (d[@"im"]) { // if we have a picture, supply it
- cell.imageView.image = d[@"im"];
- } else if (!d[@"task"]) { // if not, download it
- cell.imageView.image = nil;
- NSURLSessionTask* task = [self.downloader download:d[@"picurl"]
- completionHandler:^(NSURL* url){
- if (!url)
- return;
- NSData* data = [NSData dataWithContentsOfURL:url];
- UIImage* im = [UIImage imageWithData:data];
- d[@"im"] = im;
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
- });
- }];
- }
- return cell;
- }
- - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement