iOS开发中,常见遇到的问题

解决一:iOS如何将父视图透明,而内容不透明的方法
  self.view.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.7f];
  我们只设置了背景是透明的,没有全局的设置view的透明属性,就能使得添加到view的所有子试图保持原来的属性,不会变成透明的
 
 
解决二:如何设置label上面的文字显示不同颜色
 //label上的文字
NSString * str = [NSString stringWithFormat:@"%d/%lu",i,(unsigned long)_picArr.count];
//label的颜色
label.textColor = [UIColor greenColor];
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:str];
NSRange redRange = NSMakeRange(0, [[noteStr string] rangeOfString:@"/"].location);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
[label setAttributedText:noteStr];
最终样式:      1/4   2/4  等等
 
解决三:iOS开发中,压缩图片的方法(2种方式)
方法一:
-(UIImage*)imageWithImageSimple:(UIImage*)images scaledToSize:(CGSize)newSize{
  UIGraphicsBeginImageContext(newSize);
  [images drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return newImage;
}
 
方法二:
-(UIImage *)thumbnailFromImage:(UIImage*)image{
  CGSize originImageSize = image.size;
  CGRect newRect = CGRectMake(0, 0, 100, 100);
  UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
  //(原图的宽高做分母,导致大的结果比例更小,做MAX后,ratio*原图长宽得到的值最小是40,最大则比40大,这样的好处是可以让原图在画进40*40的缩略矩形画布时,origin可以取=(缩略矩形长宽减原图长宽*ratio)/2 ,这样可以得到一个可能包含负数的origin,结合缩放的原图长宽size之后,最终原图缩小后的缩略图中央刚好可以对准缩略矩形画布中央)
  float rotio = MAX(newRect.size.width/originImageSize.width, newRect.size.height/originImageSize.height);
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];
  //剪裁上下文
  [path addClip];
  //让image在缩略图居中() 
  CGRect projectRect;
  projectRect.size.width = originImageSize.width* rotio;
  projectRect.size.height = originImageSize.height * rotio;
  projectRect.origin.x= (newRect.size.width- projectRect.size.width) /2;
  projectRect.origin.y= (newRect.size.height- projectRect.size.height) /2;
    //在上下文画图
  [image drawInRect:projectRect];
  UIImage *thumnailImg = UIGraphicsGetImageFromCurrentImageContext();
  //结束绘制图
  UIGraphicsEndImageContext(); 
  return thumnailImg;
}
 
解决四:获取当前时间
  一:时间为这种格式为时间戳(秒)
      //获取当前时间的时间戳
  NSDate *date = [NSDate date];
  NSString * currentTime = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    NSLog(@"获取当前时间时间戳  = %@",currentTime);
 
  二: 时间为这种格式:YYYY-MM-dd HH:mm(大写H代表24小时制,小写h代表12小时制);
  //初始化格式器
  NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  //定义时间为这种格式:YYYY-MM-dd HH:mm
  [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
      //将NSDate *对象 转化为 NSString *对象。
  NSString *currentTime = [formatter stringFromDate:[NSDate date]];
 
持续更新中...
posted @ 2017-05-16 14:00  KennyHito  阅读(312)  评论(0编辑  收藏  举报