【问题标题】:get thumbnail from UIImagePickerController when using CAMERA (not album)使用 CAMERA 时从 UIImagePickerController 获取缩略图(不是专辑)
【发布时间】:2014-02-25 21:27:59
【问题描述】:

将 ALBUM 与 UIImagePickerController 一起使用时,很容易获得缩略图。

甚至可以轻松地从视频中获取缩略图。

但是当你使用 CAMERA 时,

self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;

然后你回到

-(void)imagePickerController:(UIImagePickerController *)picker
       didFinishPickingMediaWithInfo:(NSDictionary *)info

如何快速获得缩略图?

注意,我并不是特别想保存图像。我想使用相机中的图像(想象一下,然后在绘图屏幕中使用它,或用于图像处理等)

下面是一个简单的类别,它将图像转换为 128.128 图像。但是速度太慢了。

许多应用程序非常快速地从相机移动到下一个屏幕(例如,编辑图像或在其上绘图)。什么技术?干杯

-(UIImage *)squareAndSmall
{
// this category works fine to make a 128.128 thumbnail,
// but it is very slow
CGSize finalsize = CGSizeMake(128,128);

CGFloat scale = MAX(
  finalsize.width/self.size.width,
  finalsize.height/self.size.height);
CGFloat width = self.size.width * scale;
CGFloat height = self.size.height * scale;

//uses the central area, say....
CGRect imageRect = CGRectMake(
  (finalsize.width - width)/2.0f,
  (finalsize.height - height)/2.0f,
  width, height);

UIGraphicsBeginImageContextWithOptions(finalsize, NO, 0);
[self drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;
}

【问题讨论】:

    标签: ios iphone ios7 camera uiimagepickercontroller


    【解决方案1】:

    尝试使用“AssetLibrary.framework”将图像写入您的相机胶卷,它会自动为您创建一个缩略图表示。此外,由于这个过程是异步的,它肯定会提升应用的性能。

    尝试以下方法:

    #import <AssetsLibrary/AssetsLibrary.h>
    
    ////.........your code
    
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        /////.........
        UIImage* img = info[UIImagePickerControllerOriginalImage];
        ALAssetsLibrary *library = [ALAssetsLibrary new];  
        [library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){  
            if (error) {  
                NSLog(@"error");  
                return;
            } 
    
            [library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                [self.imageViewer setImage:[UIImage imageWithCGImage:[asset aspectRatioThumbnail]]]; //self.imageViewer is a UIImageView to display thumbnail
            } failureBlock:^(NSError *error) {
                NSLog(@"Failed to load captured image.");
            }];
        }];  
        [library release];
    
        //................
    }
    
    ////......rest of your code
    

    我还没有测试过代码,但我希望它能满足你的目的。请确保在您尝试之前将“AssetLibrary.framework”的引用添加到您的项目中,并让我知道它是否适合您。

    【讨论】:

    • 顺便说一句,Ayan,我根本不保存图像,也不想保存图像 - 我只想即时获得缩略图。但是感谢您在这里提供的信息丰富的答案!干杯
    • @Joe 不幸的是,资产库仅在写入过程中生成缩略图和任何其他相关图像(fullResolutionImage/fullScreenImage) :(
    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 2011-08-08
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2015-12-17
    相关资源
    最近更新 更多