很多UIImage的方法可以写成分类,方便调用。
- 将图片链接转为UIImage
/**
根据url生成图片
@param fileURL 图片url
@return 图片
*/
+(UIImage *) getImageFromURL:(NSString *)fileURL{
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
- 图片转base64字符串
/**
图片转成base64
@param image 图片
@return base64
*/
+ (NSString *)imageToString:(UIImage *)image{
NSData *imagedata = UIImagePNGRepresentation(image);
NSString *image64 = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
return image64;
}
- base64字符串转图片
/**
根据base64转成图片
@param base64Image base64
@return 图片
*/
+(UIImage*)stringToImage:(NSString*)base64Image{
NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:base64Image options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
NSData *imgData = UIImagePNGRepresentation(decodedImage);
UIImage *img = [UIImage imageWithData:imgData];
return img;
}
- 通过颜色值转图片
/**
根据颜色生成图片
@param color 颜色
@param rect 图片尺寸
@return 图片
*/
+ (UIImage *)cl_imageWithColor:(NSString *)color andRect:(CGRect)rect{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (color.length == 0 ) {//背景透明色
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
}else{
CGContextSetFillColorWithColor(context, [hexStringToColor(color) CGColor]);
}
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- 视图生成图片
/**
根据视图生成图片
@param view 视图
@param size 视图尺寸
@return 图片
*/
+ (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
/*第一个参数表示区域大小。
第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。
第三个参数就是屏幕密度了,关键就是第三个参数 [UIScreen mainScreen].scale。
另:第二个参数改为yes,可以解决图片保存相册的右侧和底部的白边问题*/
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- gif图片转数组
/**
将gif的data数据转成u图片数组
@param data gif的data数据
@return 图片数组
*/
+( NSMutableArray*) praseGIFDataToImageArray:(NSData *)data{
NSMutableArray *frames = [[NSMutableArray alloc] init];
CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL); CGFloat animationTime = 0.f;
if (src) {
size_t l = CGImageSourceGetCount(src);
frames = [NSMutableArray arrayWithCapacity:l];
for (size_t i = 0; i < l; i++) {
CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
NSDictionary *properties = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
animationTime += [delayTime floatValue];
if (img) {
[frames addObject:[UIImage imageWithCGImage:img]];
CGImageRelease(img);
}
}
CFRelease(src);
}
return frames;
}
- 对某个view进行截图,截屏
/**
截屏
@param view 截屏的视图
@return 图片
*/
+(UIImage *)captureImageFromView:(UIView *)view {
CGRect viewRect = [view bounds];
CGFloat scale = 1.0;
if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
CGFloat tmp = [[UIScreen mainScreen]scale];
if (tmp > 1.5) {
scale = 2.0;
}
if (tmp > 2.5) {
scale=3.0;
}
}
if (scale > 1.5) {
UIGraphicsBeginImageContextWithOptions(viewRect.size, NO, scale);
} else {
UIGraphicsBeginImageContext(viewRect.size);
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- 获取启动页的图片
/**
获取启动页
@return 启动页图片
*/
+(UIImage*)getLauchImage{
CGSize viewSize = [UIScreen mainScreen].bounds.size;
NSString *viewOrientation = @"Portrait";
UIImage *lauchImage = nil;
NSArray *imageDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary *dict in imageDict) {
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
lauchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
}
}
return lauchImage;
}
- 根据质量或者尺寸压缩图片
/**
压缩图片
@param image 图片
@param maxLength 最大质量
@return 图片
*/
+ (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
// Compress by quality
CGFloat compression = 1;
NSData *data = UIImageJPEGRepresentation(image, compression);
NSLog(@"%lukb",data.length/1024);
if (data.length < maxLength) return image;
CGFloat max = 1;
CGFloat min = 0;
for (int i = 0; i < 6; ++i) {
compression = (max + min) / 2;
data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
UIImage *resultImage = [UIImage imageWithData:data];
if (data.length < maxLength) return resultImage;
// Compress by size
NSUInteger lastDataLength = 0;
while (data.length > maxLength && data.length != lastDataLength) {
lastDataLength = data.length;
CGFloat ratio = (CGFloat)maxLength / data.length;
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
(NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
UIGraphicsBeginImageContext(size);
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(resultImage, compression);
}
return resultImage;
}
持续更新中...