Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  2. {
  3. return 1;
  4. }
  5.  
  6.  
  7. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  8. {
  9. return [[self objects] count];
  10. }
  11.  
  12. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  13. {
  14. NSArray *people = [self objects];
  15. static NSString *CellIdentifier = @"Cell";
  16.  
  17. VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
  18.  
  19.  
  20. PFObject *current;
  21.  
  22. current = [people objectAtIndex:indexPath.item];
  23.  
  24. PFFile *userImageFile = current[@"image"];
  25.  
  26. [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
  27. UIImage *image = [UIImage imageWithData:imageData];
  28.  
  29. [[cell contentView] setContentMode: UIViewContentModeScaleAspectFit];
  30. [[cell imageView] setImage:image];
  31.  
  32. }];
  33.  
  34. [[cell title] setText:[current valueForKey:@"title"]];
  35. [[cell price] setText:[NSString stringWithFormat: @"£%@", [current valueForKey:@"price"]]];
  36.  
  37. return cell;
  38.  
  39. }
  40.  
  41. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  42. {
  43.  
  44. NSArray *people = [self objects];
  45.  
  46. static NSString *CellIdentifier = @"Cell";
  47.  
  48. VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
  49.  
  50.  
  51.  
  52. PFObject *current;
  53.  
  54. current = [people objectAtIndex:indexPath.item];
  55.  
  56.  
  57.  
  58. PFFile *userImageFile = current[@"image"];
  59.  
  60. [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
  61.  
  62. if (!error)
  63. {
  64.  
  65. if (!image) {
  66. [[cell imageView] setImage:[UIImage imageNamed:@"placeholder.png"]];
  67.  
  68.  
  69. } else {
  70.  
  71. image = [UIImage imageWithData:imageData];
  72.  
  73.  
  74. //resize image
  75. CGSize destinationSize = CGSizeMake(158,187);
  76. UIGraphicsBeginImageContext(destinationSize);
  77. [image drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
  78.  
  79. //New image
  80. UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
  81. UIGraphicsEndImageContext();
  82.  
  83. //Optimise image
  84. NSData *imageDataCompressed = UIImageJPEGRepresentation(newImage, 0.4f);
  85. // NSLog(@"Image Size %@", NSStringFromCGSize(newImage.size));//log size of image
  86. NSLog(@"%@", [current valueForKey:@"title"]);
  87.  
  88.  
  89. [[cell imageView] setImage:[UIImage imageWithData:imageDataCompressed]];
  90.  
  91.  
  92.  
  93. }
  94.  
  95. }
  96.  
  97. }];
  98.  
  99.  
  100. [[cell title] setText:[current valueForKey:@"title"]];
  101. [[cell price] setText:[NSString stringWithFormat: @"£%@", [current valueForKey:@"price"]]];
  102.  
  103.  
  104. return cell;
  105.  
  106. }
  107.  
  108. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  109. {
  110.  
  111. NSArray *people = [self objects];
  112.  
  113. static NSString *CellIdentifier = @"Cell";
  114.  
  115. VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
  116.  
  117. [cell.activityIndicator startAnimating];
  118.  
  119. PFObject *current;
  120.  
  121. current = [people objectAtIndex:indexPath.item];
  122.  
  123.  
  124.  
  125. PFFile *userImageFile = current[@"image"];
  126.  
  127. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:userImageFile.url, indexPath.item]];
  128. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataDontLoad
  129. timeoutInterval:6.0];
  130. [cell.imageView setImageWithURLRequest:urlRequest
  131. placeholderImage:[UIImage imageNamed:@"placeholder.png"]
  132. success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
  133.  
  134. //resize image
  135. CGSize destinationSize = CGSizeMake(158,187);
  136. UIGraphicsBeginImageContext(destinationSize);
  137. [image drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
  138.  
  139. //New image
  140. UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
  141. UIGraphicsEndImageContext();
  142.  
  143. //Optimise image
  144. NSData *imageDataCompressed = UIImageJPEGRepresentation(newImage, 0.4f);
  145.  
  146. cell.imageView.image = [UIImage imageWithData:imageDataCompressed];
  147. NSLog(@"Image Size %@", NSStringFromCGSize(newImage.size));//log size of image
  148. [cell.activityIndicator stopAnimating];
  149.  
  150. } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
  151. NSLog(@"Failed to download image: %@", error);
  152. }];
  153.  
  154.  
  155.  
  156.  
  157. return cell;
  158.  
  159. }
  160.  
  161. - (void)grabDataFromCloud
  162. {
  163.  
  164. self.model = [NSMutableArray array];
  165. for (PFObject *object in [self objects]) {
  166. PFFile *imageFile = [object valueForKey:@"image"];
  167. NSURL *url = [NSURL URLWithString:imageFile.url];
  168.  
  169. NSMutableDictionary *newObject = [NSMutableDictionary dictionaryWithDictionary:@{@"title": [object valueForKey:@"title"], @"price": [object valueForKey:@"price"], @"imageUrl": url}];
  170. [[self model] addObject:newObject];
  171. }
  172. }
  173.  
  174. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  175. {
  176.  
  177. [self grabDataFromCloud];
  178. static NSString *CellIdentifier = @"Cell";
  179.  
  180. VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
  181.  
  182. [cell.activityIndicator setHidden:YES];
  183.  
  184. NSMutableDictionary* d = [self.model objectAtIndex:indexPath.item];
  185. cell.title.text = d[@"title"];
  186. cell.price.text = [NSString stringWithFormat:@"£%@", d[@"price"]];
  187.  
  188. if (d[@"image"]) {
  189. cell.imageView.image = d[@"image"];
  190. } else { // if not, download it
  191. cell.imageView.image = nil;
  192.  
  193. dispatch_queue_t backgroundQueue = dispatch_queue_create("test", 0);
  194.  
  195. dispatch_async(backgroundQueue, ^{
  196.  
  197. NSData* data = [NSData dataWithContentsOfURL:d[@"imageUrl"]];
  198. UIImage* img = [UIImage imageWithData:data];
  199. d[@"image"] = img;
  200. dispatch_async(dispatch_get_main_queue(), ^{
  201. //causes crash Assertion failure in -[UICollectionView _endItemAnimations],
  202. // /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3687
  203. // [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
  204. });
  205. });
  206.  
  207. }
  208.  
  209. return cell;
  210. }
  211.  
  212. - (UITableViewCell *)tableView:(UITableView *)tableView
  213. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  214. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  215. NSMutableDictionary* d = (self.model)[indexPath.row];
  216. cell.textLabel.text = d[@"text"];
  217. if (d[@"im"]) { // if we have a picture, supply it
  218. cell.imageView.image = d[@"im"];
  219. } else if (!d[@"task"]) { // if not, download it
  220. cell.imageView.image = nil;
  221. NSURLSessionTask* task = [self.downloader download:d[@"picurl"]
  222. completionHandler:^(NSURL* url){
  223. if (!url)
  224. return;
  225. NSData* data = [NSData dataWithContentsOfURL:url];
  226. UIImage* im = [UIImage imageWithData:data];
  227. d[@"im"] = im;
  228. dispatch_async(dispatch_get_main_queue(), ^{
  229. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  230. });
  231. }];
  232. }
  233. return cell;
  234. }
  235.  
  236. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement