使用此代码选择图像....
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
else
{
[self showMessage:@"This device doesn't support photo libraries."
withTitle:@"Error"];
}
使用下面的代码合并压缩
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
// to get the image image name use the following code
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(@"[imageRep filename] : %@", [imageRep filename]);
[_imageNameArray addObject:[imageRep filename]];
};
[_uploadTbleView reloadData];
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imgData = UIImageJPEGRepresentation(selectedImage, 1.0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// [self.baseArray addObject:[self encodeToBase64String: selectedImage]];
[self.baseArray addObject:[self base64forData:imgData]];
// NSLog(@"%@",[self encodeToBase64String: selectedImage]);
});
[self.imageSizeArray addObject: [NSNumber numberWithInteger:[imgData length]]];
NSLog(@"Size of Image(bytes):%ld",[imgData length]);
}
以下是代码将图像压缩到base64 bef矿石被上传
- (NSString*)base64forData:(NSData*) theData
{
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2)/3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i/3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
注:
你又后端有这个的base64数据进行解码得到的图像内容。如果有任何问题,再打电话给我,我会帮助你
检查我的答案兄弟 –