UzysAssetsViewCell.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // UzysAssetsViewCell.m
  3. // UzysAssetsPickerController
  4. //
  5. // Created by Uzysjung on 2014. 2. 12..
  6. // Copyright (c) 2014년 Uzys. All rights reserved.
  7. //
  8. // 版权属于原作者
  9. // http://code4app.com(cn) http://code4app.net(en)
  10. // 来源于最专业的源码分享网站: Code4App
  11. #import "UzysAssetsViewCell.h"
  12. @interface UzysAssetsViewCell()
  13. @property (nonatomic, strong) ALAsset *asset;
  14. @property (nonatomic, strong) UIImage *image;
  15. @property (nonatomic, copy) NSString *type;
  16. @property (nonatomic, copy) NSString *title;
  17. @property (nonatomic, strong) UIImage *videoImage;
  18. @end
  19. @implementation UzysAssetsViewCell
  20. static UIFont *videoTimeFont = nil;
  21. static CGFloat videoTimeHeight;
  22. static UIImage *videoIcon;
  23. static UIColor *videoTitleColor;
  24. static UIImage *checkedIcon;
  25. static UIImage *uncheckedIcon;
  26. static UIColor *selectedColor;
  27. + (void)initialize
  28. {
  29. videoTitleColor = [UIColor whiteColor];
  30. videoTimeFont = [UIFont systemFontOfSize:12];
  31. videoTimeHeight = 20.0f;
  32. videoIcon = [UIImage imageNamed:@"UzysAssetPickerController.bundle/uzysAP_ico_assets_video"];
  33. checkedIcon = [UIImage imageNamed:@"UzysAssetPickerController.bundle/uzysAP_ico_photo_thumb_check"];
  34. uncheckedIcon = [UIImage imageNamed:@"UzysAssetPickerController.bundle/uzysAP_ico_photo_thumb_uncheck"];
  35. selectedColor = [UIColor colorWithWhite:1 alpha:0.3];
  36. }
  37. - (id)initWithFrame:(CGRect)frame
  38. {
  39. self = [super initWithFrame:frame];
  40. if (self)
  41. {
  42. // Initialization code
  43. self.opaque = YES;
  44. }
  45. return self;
  46. }
  47. - (void)applyData:(ALAsset *)asset
  48. {
  49. self.asset = asset;
  50. self.image = [UIImage imageWithCGImage:asset.thumbnail];
  51. self.type = [asset valueForProperty:ALAssetPropertyType];
  52. self.title = [UzysAssetsViewCell getTimeStringOfTimeInterval:[[asset valueForProperty:ALAssetPropertyDuration] doubleValue]];
  53. }
  54. - (void)setSelected:(BOOL)selected
  55. {
  56. [super setSelected:selected];
  57. [self setNeedsDisplay];
  58. if(selected)
  59. {
  60. [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction animations:^{
  61. self.transform = CGAffineTransformMakeScale(0.97, 0.97);
  62. } completion:^(BOOL finished) {
  63. [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction animations:^{
  64. self.transform = CGAffineTransformIdentity;
  65. } completion:^(BOOL finished) {
  66. }];
  67. }];
  68. }
  69. else
  70. {
  71. [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction animations:^{
  72. self.transform = CGAffineTransformMakeScale(1.03, 1.03);
  73. } completion:^(BOOL finished) {
  74. [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction animations:^{
  75. self.transform = CGAffineTransformIdentity;
  76. } completion:^(BOOL finished) {
  77. }];
  78. }];
  79. }
  80. }
  81. - (void)drawRect:(CGRect)rect
  82. {
  83. // Image
  84. [self.image drawInRect:CGRectMake(0, 0, kThumbnailLength, kThumbnailLength)];
  85. // Video title
  86. if ([self.type isEqual:ALAssetTypeVideo])
  87. {
  88. // Create a gradient from transparent to black
  89. CGFloat colors [] =
  90. {
  91. 0.0, 0.0, 0.0, 0.0,
  92. 0.0, 0.0, 0.0, 0.8,
  93. 0.0, 0.0, 0.0, 1.0
  94. };
  95. CGFloat locations [] = {0.0, 0.75, 1.0};
  96. CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
  97. CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, locations, 2);
  98. CGColorSpaceRelease(baseSpace);
  99. CGContextRef context = UIGraphicsGetCurrentContext();
  100. CGFloat height = rect.size.height;
  101. CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), height - videoTimeHeight);
  102. CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
  103. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
  104. NSDictionary *attributes = @{NSFontAttributeName:videoTimeFont,NSForegroundColorAttributeName:videoTitleColor};
  105. CGSize titleSize = [self.title sizeWithAttributes:attributes];
  106. [self.title drawInRect:CGRectMake(rect.size.width - (NSInteger)titleSize.width - 2 , startPoint.y + (videoTimeHeight - 12) / 2, kThumbnailLength, height) withAttributes:attributes];
  107. CGGradientRelease(gradient);
  108. [videoIcon drawAtPoint:CGPointMake(2, startPoint.y + (videoTimeHeight - videoIcon.size.height) / 2)];
  109. }
  110. if (self.selected)
  111. {
  112. CGContextRef context = UIGraphicsGetCurrentContext();
  113. CGContextSetFillColorWithColor(context, selectedColor.CGColor);
  114. CGContextFillRect(context, rect);
  115. [checkedIcon drawAtPoint:CGPointMake(CGRectGetMaxX(rect) - checkedIcon.size.width -2, CGRectGetMinY(rect)+2)];
  116. }
  117. else
  118. {
  119. [uncheckedIcon drawAtPoint:CGPointMake(CGRectGetMaxX(rect) - uncheckedIcon.size.width -2, CGRectGetMinY(rect)+2)];
  120. }
  121. }
  122. + (NSString *)getTimeStringOfTimeInterval:(NSTimeInterval)timeInterval
  123. {
  124. NSCalendar *calendar = [NSCalendar currentCalendar];
  125. NSDate *dateRef = [[NSDate alloc] init];
  126. NSDate *dateNow = [[NSDate alloc] initWithTimeInterval:timeInterval sinceDate:dateRef];
  127. unsigned int uFlags =
  128. NSSecondCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit |
  129. NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
  130. NSDateComponents *components = [calendar components:uFlags
  131. fromDate:dateRef
  132. toDate:dateNow
  133. options:0];
  134. NSString *retTimeInterval;
  135. if (components.hour > 0)
  136. {
  137. retTimeInterval = [NSString stringWithFormat:@"%ld:%02ld:%02ld", (long)components.hour, (long)components.minute, (long)components.second];
  138. }
  139. else
  140. {
  141. retTimeInterval = [NSString stringWithFormat:@"%ld:%02ld", (long)components.minute, (long)components.second];
  142. }
  143. return retTimeInterval;
  144. }
  145. @end