UIPickerView无法正确显示包含图像的自定义视图

UIPickerView无法正确显示包含图像的自定义视图

本文介绍了iOS7 UIPickerView无法正确显示包含图像的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题始于iOS7中使用新的UIPickerView控制器。要在UIPickerView控制器中使用图像,必须使用委托方法返回图像:

This issue started happening in iOS7 with the new UIPickerView controller. To use images in your UIPickerView controller you must use the delegate method to return an image:

pickerView:viewForRow:forComponent:reusingView:

pickerView:viewForRow:forComponent:reusingView:

问题在于屏幕随后出现各种奇怪的行为 - 当您在控件上下移动手指时,图像视图会消失。

The problem is that the screen subsequently exhibits all kinds of strange behavior - the image views disappear as you move your finger up and down the control.

推荐答案

这是一个发布在iOS 7.0.2上的开发论坛上的解决方案:

This is a solution posted on a dev forum which works as of iOS 7.0.2:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    // self.myImages is an array of UIImageView objects
    UIView * myView = [self.myImages objectAtIndex:row];

    // first convert to a UIImage
    UIGraphicsBeginImageContextWithOptions(myView.bounds.size, NO, 0);

    [myView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // then convert back to a UIImageView and return it
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    return imageView;
}

这篇关于iOS7 UIPickerView无法正确显示包含图像的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:18