UIImage分類擴(kuò)展(壓縮圖片/截圖/獲取啟動頁/base64轉(zhuǎn)換等)

很多UIImage的方法可以寫成分類,方便調(diào)用。

  • 將圖片鏈接轉(zhuǎn)為UIImage
/**
 根據(jù)url生成圖片

 @param fileURL 圖片url
 @return 圖片
 */
+(UIImage *) getImageFromURL:(NSString *)fileURL{
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    return result;
}
  • 圖片轉(zhuǎn)base64字符串
/**
 圖片轉(zhuǎn)成base64

 @param image 圖片
 @return base64
 */
+ (NSString *)imageToString:(UIImage *)image{
    NSData *imagedata = UIImagePNGRepresentation(image);
    NSString *image64 = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return image64;
}
  • base64字符串轉(zhuǎn)圖片
/**
 根據(jù)base64轉(zhuǎn)成圖片

 @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;
}
  • 通過顏色值轉(zhuǎn)圖片
/**
 根據(jù)顏色生成圖片

 @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;
}
  • 視圖生成圖片
/**
 根據(jù)視圖生成圖片

 @param view 視圖
 @param size 視圖尺寸
 @return 圖片
 */
+ (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    /*第一個參數(shù)表示區(qū)域大小。
     第二個參數(shù)表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。
     第三個參數(shù)就是屏幕密度了,關(guān)鍵就是第三個參數(shù) [UIScreen mainScreen].scale。
     另:第二個參數(shù)改為yes,可以解決圖片保存相冊的右側(cè)和底部的白邊問題*/
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
  • gif圖片轉(zhuǎn)數(shù)組
/**
 將gif的data數(shù)據(jù)轉(zhuǎn)成u圖片數(shù)組

 @param data gif的data數(shù)據(jù)
 @return 圖片數(shù)組
 */
+( 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進(jìn)行截圖,截屏
/**
 截屏

 @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;
}
  • 根據(jù)質(zhì)量或者尺寸壓縮圖片
/**
 壓縮圖片

 @param image 圖片
 @param maxLength 最大質(zhì)量
 @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;
}

持續(xù)更新中...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容