最近公司開發(fā)遇到一個需求, 需要判斷照片的大小 不能超過2M,從網(wǎng)上找了好多方法,都發(fā)現(xiàn)計算出來的結(jié)果和實際大小不一樣。后來發(fā)現(xiàn)TZImagePickerController里有相關(guān)類似方法氯材,于是參考著,抽取出來硝岗,作為筆記氢哮。話不多說,直接上代碼型檀。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSDictionary *meta = [info objectForKey:UIImagePickerControllerMediaMetadata];
__block NSInteger dataLength = 0;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.networkAccessAllowed = YES;
//self.location 是CLLocation類型 系統(tǒng)定位得到
WeakSelf
[self getImageAssetWithImage:image meta:meta location:self.location completion:^(PHAsset *asset, NSError *error) {
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
dataLength += imageData.length;
NSString *bytes = [self getBytesFromDataLength:dataLength];//bytes就是計算出來的照片大小
}];
}];
_uploadImage = image;
_uploadImageView.image = image;
[self.imagePicker dismissViewControllerAnimated:YES completion:nil];
[self commitButtonStatusSet];
}
- (void)getImageAssetWithImage:(UIImage *)image meta:(NSDictionary *)meta location:(CLLocation *)location completion:(void (^)(PHAsset *asset, NSError *error))completion {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"];
NSString *path = [NSTemporaryDirectory() stringByAppendingFormat:@"image-%@.jpg", [formater stringFromDate:[NSDate date]]];
NSURL *tmpURL = [NSURL fileURLWithPath:path];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)tmpURL, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)meta);
CGImageDestinationFinalize(destination);
CFRelease(source);
CFRelease(destination);
__block NSString *localIdentifier = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:tmpURL];
localIdentifier = request.placeholderForCreatedAsset.localIdentifier;
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} completionHandler:^(BOOL success, NSError *error) {
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
if (success && completion) {
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil] firstObject];
completion(asset, nil);
} else if (error) {
NSLog(@"保存照片出錯:%@",error.localizedDescription);
if (completion) {
completion(nil, error);
}
}
});
}];
}
- (NSString *)getBytesFromDataLength:(NSInteger)dataLength {
NSString *bytes;
if (dataLength >= 0.1 * (1024 * 1024)) {
bytes = [NSString stringWithFormat:@"%0.1fM",dataLength/1024/1024.0];
} else if (dataLength >= 1024) {
bytes = [NSString stringWithFormat:@"%0.0fK",dataLength/1024.0];
} else {
bytes = [NSString stringWithFormat:@"%zdB",dataLength];
}
return bytes;
}
最后感謝TZImagePickerController作者提供的方法和思路