一
计算text的高度
/******************** 额外的辅助属性 **********************/ /* cell的高度 */ @property (nonatomic, assign, readonly) CGFloat cellHeight;
- (CGFloat)cellHeight { if (!_cellHeight) { //文字Y值 CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT); //计算文字的高度 CGFloat textH =[self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height; _cellHeight = BSTopicCellMargin * 2 + BSTopicCellTextY + BSTopicCellBottomBarH + textH +self.height; } return _cellHeight; }
二
只计算一次cell的高度
将cell的高度作为模型本身的数据,只需要在模型中设置一次即可
propery编译器会自动生成getter和setter以及下划线的成员变量,但是若用户重构getter和setter时,编译器默认不在生成下划线成员变量,需要手动添加
@interface BSTopic() { CGFloat _cellHeight; } @end
三
1 当模型中的属性与服务器返回的key值不一致时
+ (NSDictionary *)replacedKeyFromProperyName { return @{ @"small_image" : @"image0", @"middle_image" : @"image2", @"large_image" : @"image1", }; }
2 在模型中计算frame
3 如果设定的尺寸和显示出来的效果不一致
- (void)awakeFromNib { //设定的尺寸 和 显示的尺寸不一致,可能是此处默认的随着视图的拉伸而改变 self.autoresizingMask = UIViewAutoresizingNone; }
4 iphone不支持播放gif图片,ImageIO框架会将gif图片分解为若干个uiimage,然后显示出来
5 可以使用懒加载,防止重复创建
//懒加载 防止重复创建 - (BSTopicPictureView *)pictuerView { if (!_pictuerView) { BSTopicPictureView *pictuerView = [BSTopicPictureView pictureView]; [self.contentView addSubview:pictuerView]; _pictuerView = pictuerView; } return _pictuerView; }
四
五
1 使用图片扩展名拿到图片的真实类型
//设置是否gif NSString *extension = topic.large_image.pathExtension;//拿到扩展名 //转成小写判断 不是gif的时候隐藏 self.gifView.hidden = ![extension.lowercaseString isEqualToString:@"gif"];
2 在不知道图片扩展名的情况下,如何知道图片的真实类型
取出图片的第一个字节,就可以判断出图片的真实类型
0xFF JPEG
0x89 PNG
0X47 GIF
0X49
0X4D TIFF
0x52 WEBP/RIFF
3 设计图片自动剪切
六
使用第三方框架圆形进度条DALabeledCircularProgressView
#import <DALabeledCircularProgressView.h>
self.progressView.roundedCorners = 5; self.progressView.progressLabel.textColor = [UIColor whiteColor];
//设置图片 [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.large_image] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { self.progressView.hidden = NO; CGFloat progress = 1.0 * receivedSize / expectedSize; [self.progressView setProgress:progress]; self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.0f%%", progress * 100]; } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { self.progressView.hidden = YES; }];
七
1 减少使用第三方框架的风险:
自定义一个类,继承自第三方框架即可
2 给图片添加监听器
//给图片添加监听器 self.imageView.userInteractionEnabled = YES; [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicture)]];
3 只有控制器可以弹出控制器,如果是UIView弹出控制器,必须先拿到根控制器
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicture animated:YES completion:nil];
4 禁止按钮被点击
保证按钮保持以前的地方不能点击
按钮变灰
八
1 float在经过函数运行后,出现了不为数字的值 nan:not a number
常见原因:除以0
sizeWithFont的字符串为nil
数学函数的不正确运算
链接:http://www.jianshu.com/p/f9c5b073304f
2 视图转换
presentViewController 和 dissViewController 是一对
push 和 pop 是一对
- (IBAction)showPicture { //显示完整图片 BSShowPictureViewController *showPicture = [[BSShowPictureViewController alloc] init]; showPicture.topic = self.topic; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicture animated:NO completion:nil]; }
- (IBAction)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
3 保存图片到本地相册
在原文件中提示:
// Adds a photo to the saved photos album. The optional completionSelector should have the form: // - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
- (IBAction)save { //将图片写入相册 UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; { if(error) { [SVProgressHUD showSuccessWithStatus:@"保存失败!"]; }else { [SVProgressHUD showSuccessWithStatus:@"保存成功!"]; } }
九
在使用SVProgress显示加载进度时,cell的循环利用会导致progressView的receivedSize是同一个对象,即点击新的cell时,因为网速慢,会显示上一个cell的加载进度
可以在topic中添加一个辅助属性,来记录进度值
十
解决图片在cell中显示中间部分 -- > 将图片绘制到图形上下文中
//只对大图片进行处理 if (topic.isBigPicture == NO) return; //开启图形上下文 UIGraphicsBeginImageContextWithOptions(topic.pictureFrame.size, YES, 0.0); //将下载完的image绘制到图形上下文中 CGFloat width = topic.pictureFrame.size.width; [image drawInRect:CGRectMake(0, 0, width, image.size.height)]; //获得图片 self.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //结束图形上下文 UIGraphicsEndImageContext();