欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

开发过程中小的知识点梳理(三)

程序员文章站 2022-06-22 18:16:57
...

1.通过NSURL获取的图片->NSData->UIImage并进行压缩

UIImage *image = nil;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:weakSelf.shareInfoModel.image]];
image = [UIImage imageWithData:imageData];
if (!image) {
    image = [UIImage imageNamed:@"app_icon_90"];
}
content.imageData = UIImageJPEGRepresentation(image, 1.0);

2.定时器的简单使用。

//初始化一个定时器,5分钟执行一次timerAction方法
NSTimer *timer = [NSTimer timerWithTimeInterval:60*5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];//关键代码

NSTimeInterval timeInterval = -[self.refreshDate timeIntervalSinceNow];
if (timeInterval >= 60*1) {  //时间间隔超过1分钟
    [timer setFireDate:[NSDate distantPast]]; //定时器立即执行
 } else {
    [timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:AutoRefreshTimeInterval - timeInterval]]; //定时器开始计时
}
    //[timer setFireDate:[NSDate distantFuture]];//定时器暂停
    //[timer invalidate]; //定时器销毁

- (void)timerAction {
    NSTimeInterval timeInterval = -[self.refreshDate timeIntervalSinceNow];
    if (timeInterval >= 60*1) {//自动刷新不短于1分钟
    [self loadObjectsAtPageIndex:kFirstPageIndex];
    }
}

3.给UI控件设置横向渐变色

+ (CAGradientLayer *)gradientLayerWithCornerRadius:(CGFloat)radius
                                             width:(CGFloat)width
                                            height:(CGFloat)height
                                         leftColor:(UIColor *)leftColor
                                        rightColor:(UIColor *)rightColor{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.cornerRadius = radius;
    gradientLayer.frame = CGRectMake(0, 0, width, height);
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.colors = @[(__bridge id)leftColor.CGColor,(__bridge id)rightColor.CGColor];
    return gradientLayer;
}

4.绝对布局设置指定圆角

- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii {

    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];

    self.layer.mask = shape;
}

5.相对布局设置指定圆角

- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect {

    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];

    self.layer.mask = shape;
}