UIImage

作者: 林希品 | 来源:发表于2021-11-01 11:12 被阅读0次

//uiview 转 image

+ (UIImage*)imageWithView:(UIView*)view;

+ (UIImage*)imageWithView:(UIView*)view {

   CGFloatscale = [UIScreenmainScreen].scale;

   UIGraphicsBeginImageContextWithOptions(view.frame.size,NO, scale);

    [view.layerrenderInContext:UIGraphicsGetCurrentContext()];

   UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

   returnimage;

}

Image url changeTo Nsdata

NSError *error = nil;

NSData *dataImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL] options:NSDataReadingMappedIfSafe error:&error];

/// 压缩图片大小 并不是截取图片而是按照size绘制图片

- (UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size {

    // 创建一个基于位图的上下文(context),并将其设置为当前上下文(context)

    UIGraphicsBeginImageContext(size);

    // 绘制改变大小的图片

    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];

    // 从当前context中创建一个改变大小后的图片

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    // 使当前的context出堆栈

    UIGraphicsEndImageContext();

    // 保存图片

    // UIImageWriteToSavedPhotosAlbum(scaledImage, nil, nil, nil);

    return scaledImage;

}

相关文章

网友评论

      本文标题:UIImage

      本文链接:https://www.haomeiwen.com/subject/ykvfaltx.html