這是因爲您使用的圖像的大小通常非常大,通常超過2MB。因此,在使用它之前調整圖像大小。 這裏是代碼片段:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
if (!originalImage)
return;
// Optionally set a placeholder image here while resizing happens in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Set desired maximum height and calculate width
CGFloat height = 640.0f; // or whatever you need
CGFloat width = (height/self.view.frame.size.height) * self.view.frame.size.width;
// Resize the image
UIImage * image = [originalImage resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationDefault];
//save/use the image here
UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);//for saving to camera roll
});
}
}