• iOS-项目开发1-UIImage


    UIImage+Extension

    /// 获取后的数据 a.length > b.length. 同时,使用UIIMageJPEGRepresnetation压缩图片,如果compressionQuality设置为1.0,
    得到的图片大小比原图大,测试之后,压缩比在0.7--0.8之间大致为原图的大小
    NSData *a = UIImagePNGRepresentation(UIImage *image) NSData *b = UIIMageJPEGRepresnetation(UIImage * image, CGFloat compressionQuality)

    1. 裁剪图片上的某一部分

    /// 获取指定部分的图片
    - (UIImage *)FF_AcquireSpecialSizeImage:(CGRect)rect {
        CGImageRef imageRef = [self CGImage];
        CGImageRef needImageRef = CGImageCreateWithImageInRect(imageRef, rect);
        return [UIImage imageWithCGImage:needImageRef];
    }

    2.将图片重画在某一部分,会压缩图片的质量

    /// 重画图片到指定的Size
    - (UIImage *)FF_CompressSize:(CGSize)size {
        UIImage *result = self;
        UIGraphicsBeginImageContext(size);
        [result drawInRect:CGRectMake(0, 0, size.width, size.height)];
        result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return result;  
    }

    3.将图片压缩到指定大小,压缩质量

    /**
     将图片压缩到指定的质量
     
     @param kb 1MB == 1024KB
     @return 图片压缩后的质量
      使用二分法提高效率,一般通过6次,可以达到指定的大小,也有例外,之后通过缩小尺寸来实现目标
     */
    - (NSData *)FF_CompressQualityToSpecialKB:(CGFloat)kb {
        CGFloat specialBytes = kb * 1024;
        NSData *imageDate = UIImageJPEGRepresentation(self, 1.0);
        if (imageDate.length < specialBytes) {
            return imageDate;
        }
        CGFloat min = 0;
        CGFloat max = 1;
        CGFloat compress = 1;
        for (int i = 0; i < 6; i++) {
            compress = (max + min) / 2;
            imageDate = UIImageJPEGRepresentation(self, compress);
            if (imageDate.length < specialBytes * 0.9) {
                min = compress;
            }else if (imageDate.length > specialBytes) {
                max = compress;
            }else {
                break;
            }
        }
        
        if (imageDate.length <= specialBytes) {
            return imageDate;
        }
        
        UIImage *tempImage = [UIImage imageWithData:imageDate];
        NSUInteger lastDateLength = 0;
        while (imageDate.length > specialBytes && lastDateLength != imageDate.length) {
            lastDateLength = imageDate.length;
            CGFloat tempScale = specialBytes / imageDate.length;
            CGSize tempSize = CGSizeMake((NSUInteger)(tempImage.size.width * tempScale),
                                         (NSUInteger)(tempImage.size.height * tempScale));
            UIGraphicsBeginImageContext(tempSize);
            [tempImage drawInRect:CGRectMake(0, 0, tempSize.width, tempSize.height)];
            tempImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            imageDate = UIImageJPEGRepresentation(tempImage, compress);
        }
        
        return imageDate;
    }
     

    4 通过颜色获取图片

    + (UIImage *)FF_AcquireImageFromColor:(UIColor *)color {
        UIGraphicsBeginImageContext(CGSizeMake(1, 1));
        CGContextRef ref = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(ref, color.CGColor);
        CGContextFillRect(ref, CGRectMake(0, 0, 1, 1));
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }

    5. 创建自定义的图库,并保存图片到自定义相册

    /*
    1. 导入框架 <Photo/Photo.h>
    2. PHAsssetColoction 图库
    3. PHAsset                图片集合
    4. PHPhotoLibrary      在Library中进行各种操作,创建图库,图片等
    5. PHAssetCollectionChangeRequest 创建PHAssetCollection
    6. PHAssetChangeRequest   创建PHAsset
    */
    
    - (PHAssetCollection *)FF_CreatePhotoLibrary {
        NSString *title = [[NSBundle mainBundle].infoDictionary objectForKey:(NSString *)kCFBundleNameKey];
        PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
        PHAssetCollection *needCollection = nil;
        for (PHAssetCollection *collection in result) {
            if ([collection.localizedTitle isEqualToString:title]) {
                needCollection = collection;
            }
        }
        __block NSString *localIdentifierId = nil;
        if (!needCollection) {
            [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
                localIdentifierId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
            } error:nil];
        }
        if (localIdentifierId) {
            needCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[localIdentifierId] options:nil].firstObject;
        }
        return needCollection;
    }
    
    /// 保存图片到相机相册
    - (PHFetchResult<PHAsset *> *)FF_CreatedAssets {
        __block NSString *localIdentital = nil;
        [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
            localIdentital = [PHAssetChangeRequest creationRequestForAssetFromImage:self].placeholderForCreatedAsset.localIdentifier;
        } error:nil];
        if (localIdentital) {
            return [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentital] options:nil];
        }else {
            return nil;
        }
    }
    
    /// 保存到自定义相册
    - (void)FF_SaveImageToAlbum {
        if (![self FF_PhotoLibraryAuthorization]) {
            return;
        }
        PHAssetCollection *collection = [self FF_CreatePhotoLibrary];
        PHFetchResult<PHAsset *> *result = [self FF_CreatedAssets];
        [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
            PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
            [request insertAssets:result atIndexes:[NSIndexSet indexSetWithIndex:0]];
        } error:nil];
    }
  • 相关阅读:
    236. 二叉树的最近公共祖先
    230. 二叉搜索树中第K小的元素
    221. 最大正方形
    软件构建模式之MVC框架初窥
    九度OnlineJudge之1020:最小长方形
    九度OnlineJudge之1018:统计同成绩学生人数
    九度OnlineJudge之1017:还是畅通工程
    向Python女神推荐这些年我追过的经典书籍
    最实用的10个重构小技巧排行榜,您都用过哪些呢?
    九度OnlineJudge之1014:排名
  • 原文地址:https://www.cnblogs.com/jisa/p/10451446.html
Copyright © 2020-2023  润新知