iOS應用開發,iOS-項目開發1-UIImage

 2023-10-18 阅读 24 评论 0

摘要:UIImage+Extension /// 獲取后的數據 a.length > b.length. 同時,使用UIIMageJPEGRepresnetation壓縮圖片,如果compressionQuality設置為1.0,得到的圖片大小比原圖大,測試之后,壓縮比在0.7--0.8之間大致為原圖的大小 NSData *a 

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;  
}

iOS應用開發?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];
}

?

iOS app開發。轉載于:https://www.cnblogs.com/jisa/p/10451446.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/149470.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息