通过使用低分辨率图像然后交换高分辨率图像来加速UIImagePickerController

Rob*_*ert 1 iphone cocoa-touch objective-c ipad ios

有一个关于从相机选择器加载图像的伟大维基.这让我意识到以全分辨率拍摄图像的成本.

目前,当拍摄照片时,我推动一个新的视图控制器并以全分辨率显示图像.推动视图是一个非常缓慢和波涛汹涌的体验(约1 fps!),我想要平滑.与在Instagram上挑选照片相比,我注意到他们使用低分辨率图像,然后交换完整图像.(我需要完整的res图像,因为用户应该能够缩放和平移)

我想要的想法是这样的:

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

    UIImage* fullImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    // Push a view controller and give it the image.....
}

- (void) viewDidLoad {

    CGSize smallerImageSize = _imageView.bounds;
    UIImage* smallerImage = [MyHelper quickAndDirtyImageResize:_fullImage     
                                                        toSize:smallerImageSize];

    // Set the low res image for now... then later swap in the high res
    _imageView.image = smallerImage;

    // Swap in high res image async
    // This is the part im unsure about... Im sure UIKit isn't thread-safe!
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, NULL), ^{
        _imageView.image = _fullImage;
    });
}
Run Code Online (Sandbox Code Playgroud)

我认为UIImage在使用之前不是内存映射的.因此,在给予imageView之前,它不会减慢速度.它是否正确?

我认为图像解码已经由系统异步完成,但是,在加载时,它会大大减慢手机速度.

有没有办法执行在极低优先级后台队列中显示图像所需的一些工作?

And*_*rov 5

你试图以最复杂的方式做事:)为什么不在推动视图控制器之前准备小图像并将其传递给它们?看看这段代码:

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

    UIImage *fullImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *smallImage = [fullImage imageScaledToSize:self.view.bounds];

    // Push a view controller and give it BOTH images
}

// And in your pushed view controller

- (void)viewDidLoad
{
    _imageView.image = self.smallImage;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    _imageView.image = self.fullImage;
}
Run Code Online (Sandbox Code Playgroud)

最重要的是viewDidAppear:在动画完成后立即调用,这样您就可以在这里切换图像而不用担心.