iPhone7以及iOS11以上发送拍摄的照片是hice格式的图片
所以在iPhone7以下的iOS11以下的系统手机显示白色框框!
解决方案:
PHAsset * asset = [_assetDic valueForKey:_articleData.img];//PHAsset已经事先存到字典中
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeExact;
option.networkAccessAllowed = YES;
option.synchronous = YES;//同步执行
// 从asset中获得图片
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSLog(@"---格式-- > %@",dataUTI);
NSData *imageDataW = nil;
if ([dataUTI isEqualToString:@"public.heif"] || [dataUTI isEqualToString:@"public.heic"]) {
CIImage *ciImage = [CIImage imageWithData:imageData];
CIContext *context = [CIContext context];
NSData *jpgData = [context JPEGRepresentationOfImage:ciImage colorSpace:ciImage.colorSpace options:@{}];
imageDataW = jpgData;
} else {
imageDataW = imageData;
}
if ([imageDataW length] / (1024 * 1024) > 3.0) {
imageDataW = [[UIImage imageWithData: imageDataW]compressAndResize]; //图片大于3M压缩操作,可不调用此方法
}
// self.orignalImage = [CIImage imageWithCGImage:[[UIImage imageWithData: imageDataW] CGImage]];
//图片编辑用到的CIImage
imgView.image = [UIImage imageWithData: imageDataW];
}];
图片裁剪
CGRect rectimg = CGRectMake(0,0,200,300);//图片裁剪范围
// NSLog(@"=== > %@",NSStringFromCGRect(rectimg));
CGImageRef imageRef=CGImageCreateWithImageInRect([image CGImage],rectimg);
UIImage *image1=[UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);//释放CGImageRef,否则内存升高
_faceIageView.image = image1;
环信中获取照片的位置修改:
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
//视频类型
NSURL *videoURL = info[UIImagePickerControllerMediaURL];
// video url:
// file:///private/var/mobile/Applications/B3CDD0B2-2F19-432B-9CFA-158700F4DE8F/tmp/capture-T0x16e39100.tmp.9R8weF/capturedvideo.mp4
// we will convert it to mp4 format
NSURL *mp4 = [self _convert2Mp4:videoURL];
NSFileManager *fileman = [NSFileManager defaultManager];
if ([fileman fileExistsAtPath:videoURL.path]) {
NSError *error = nil;
[fileman removeItemAtURL:videoURL error:&error];
if (error) {
NSLog(@"failed to remove file, error:%@.", error);
}
}
[self sendVideoMessageWithURL:mp4];
}else{
//图片类型
NSURL *url = info[UIImagePickerControllerReferenceURL];
if (url == nil) {
UIImage *orgImage = info[UIImagePickerControllerOriginalImage];
[self sendImageMessage:orgImage];
} else {
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeExact;
option.networkAccessAllowed = YES;
option.synchronous = YES;//同步执行
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0f) {
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
[result enumerateObjectsUsingBlock:^(PHAsset *asset , NSUInteger idx, BOOL *stop){
if (asset) {
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *data, NSString *uti, UIImageOrientation orientation, NSDictionary *dic){
NSData *imageDataW = nil;
//[str containsString:@"world"]
if ([uti isEqualToString:@"public.heif"] || [uti isEqualToString:@"public.heic"]) {
CIImage *ciImage = [CIImage imageWithData:data];
CIContext *context = [CIContext context];
NSData *jpgData = [context JPEGRepresentationOfImage:ciImage colorSpace:ciImage.colorSpace options:@{}];
imageDataW = jpgData;
} else {
imageDataW = data;
}
// if ([imageDataW length] / (1024 * 1024) > 3.0) {
// imageDataW = [[UIImage imageWithData: imageDataW] compressAndResize]; //图片大于3M压缩操作,可不调用此方法
// }
if (imageDataW != nil) {
[self sendImageMessageWithData:imageDataW];
} else {
[self showHint:NSEaseLocalizedString(@"message.smallerImage", @"The image size is too large, please choose another one")];
}
}];
}
}];
} else {
ALAssetsLibrary *alasset = [[ALAssetsLibrary alloc] init];
[alasset assetForURL:url resultBlock:^(ALAsset *asset) {
if (asset) {
ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
Byte* buffer = (Byte*)malloc((size_t)[assetRepresentation size]);
NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:(NSUInteger)[assetRepresentation size] error:nil];
NSData* fileData = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES];
[self sendImageMessageWithData:fileData];
}
} failureBlock:NULL];
}
}
}
[picker dismissViewControllerAnimated:YES completion:nil];
self.isViewDidAppear = YES;
[[EaseSDKHelper shareHelper] setIsShowingimagePicker:NO];
}