Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. -(UITableViewCell *)tableView:(UITableView *)tableView
  2. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. cell.textLabel.text = exerciseDisplayName;
  5. cell.textLabel.numberOfLines = 0;
  6. cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
  7.  
  8. [tableView setSeparatorInset:UIEdgeInsetsZero];
  9.  
  10. UtilityMethods *commonMethods = [[UtilityMethods alloc]init];
  11.  
  12. UIImage *rowImage = [commonMethods imageForRow:tempPlaceholder.bodyPart];
  13.  
  14. cell.imageView.image = rowImage;
  15.  
  16. return cell;
  17.  
  18. }
  19.  
  20. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  21. {
  22. return 96;
  23. }
  24.  
  25. - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
  26. {
  27. UIImage *sourceImage = self;
  28. UIImage *newImage = nil;
  29. CGSize imageSize = sourceImage.size;
  30. CGFloat width = imageSize.width;
  31. CGFloat height = imageSize.height;
  32. CGFloat targetWidth = targetSize.width;
  33. CGFloat targetHeight = targetSize.height;
  34. CGFloat scaleFactor = 0.0;
  35. CGFloat scaledWidth = targetWidth;
  36. CGFloat scaledHeight = targetHeight;
  37. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  38.  
  39. if (CGSizeEqualToSize(imageSize, targetSize) == NO)
  40. {
  41. CGFloat widthFactor = targetWidth / width;
  42. CGFloat heightFactor = targetHeight / height;
  43.  
  44. if (widthFactor > heightFactor)
  45. {
  46. scaleFactor = widthFactor; // scale to fit height
  47. }
  48. else
  49. {
  50. scaleFactor = heightFactor; // scale to fit width
  51. }
  52.  
  53. scaledWidth = width * scaleFactor;
  54. scaledHeight = height * scaleFactor;
  55.  
  56. // center the image
  57. if (widthFactor > heightFactor)
  58. {
  59. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  60. }
  61. else
  62. {
  63. if (widthFactor < heightFactor)
  64. {
  65. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  66. }
  67. }
  68. }
  69.  
  70. UIGraphicsBeginImageContextWithOptions(targetSize, 0, NO); // this will crop
  71.  
  72. CGRect thumbnailRect = CGRectZero;
  73. thumbnailRect.origin = thumbnailPoint;
  74. thumbnailRect.size.width = scaledWidth;
  75. thumbnailRect.size.height = scaledHeight;
  76.  
  77. [sourceImage drawInRect:thumbnailRect];
  78.  
  79. newImage = UIGraphicsGetImageFromCurrentImageContext();
  80.  
  81. if(newImage == nil)
  82. {
  83. NSLog(@"could not scale image");
  84. }
  85.  
  86. //pop the context to get back to the default
  87. UIGraphicsEndImageContext();
  88.  
  89. return newImage;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement