//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;
}
网友评论