使用CAMERA(不是专辑)时从UIImagePickerController获取缩略图

时间:2014-02-25 21:28:00

标签: ios iphone ios7 camera uiimagepickercontroller

使用带有UIImagePickerController的ALBUM时,很容易获得缩略图。

从视频中获取缩略图甚至很容易。

但是当你使用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;
}

1 个答案:

答案 0 :(得分:2)

尝试使用“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”的引用,并告诉我它是否适合您。 快乐的编码!