【问题标题】:Problem in Rendering images in UIImageView in iPhone programmingiPhone编程中UIImageView渲染图像的问题
【发布时间】:2010-04-30 08:08:19
【问题描述】:

我有一个 UIViewController 和一个 UIImageView,在 UIImageView 上我想在 2 个图像之间翻转,但我无法实现。

这是我写的代码,如果我错了请纠正我。

UIViewController* VC = [[UIViewController alloc]init];
VC.view.backgroundColor = [UIColor redColor];
UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,400)];
[imgView setImage:[UIImage imageNamed:@"Image_A.jpg"]];
[VC.view addSubview:imgView];

sleep(2);

[imgView setImage:[UIImage imageNamed:@"Image_B.jpg"]];
[VC.view addSubview:imgView];

[window addSubview:VC.view];

所以当我执行这个项目时,只有 Image_B 显示在屏幕上,而我希望 Image_A 显示然后在 sleep(2) 时,必须显示 Image_B。

我将如何使它成为可能?

谢谢。

【问题讨论】:

    标签: iphone image uiimageview rendering


    【解决方案1】:

    使用NSTimer (docs here) 代替sleep。计时器不会阻塞线程,而是返回到运行循环。这样 UIKit 可以提交视图层次结构中的更改并将它们显示在屏幕上。在计时器的回调中,您只需更改图像即可。

    【讨论】:

      【解决方案2】:

      .h

          @interface TwoImagesAppDelegate : NSObject <UIApplicationDelegate> {
              UIWindow *window;
              UIViewController* VC;
              UIImageView* imgView1;
              UIImageView* imgView2;
          }
      

      .m

          - (void) toggle
          {
              static int toggle = 0;
              if (toggle) {
                  imgView1.hidden = YES;
                  imgView2.hidden = NO;
              } else {
                  imgView1.hidden = NO;
                  imgView2.hidden = YES;
              }
              toggle = toggle ? 0 : 1;
          }
      
          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
      
              // Override point for customization after application launch
      
              [window makeKeyAndVisible];
      
              VC = [[UIViewController alloc]init];
              VC.view.backgroundColor = [UIColor redColor];
              imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
              [imgView1 setImage:[UIImage imageNamed:@"1.png"]];
              [VC.view addSubview:imgView1];
              [window addSubview:VC.view];
      
              VC.view.backgroundColor = [UIColor blackColor];
              imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
              [imgView2 setImage:[UIImage imageNamed:@"2.png"]];
              [VC.view addSubview:imgView2];  
              [window addSubview:VC.view];
      
              [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(toggle) userInfo:nil repeats:YES];
      
              return YES;
          }
      

      请注意,这个例子展示了如何使用timer,存在存在内存泄漏。

      【讨论】:

        【解决方案3】:

        我假设你在一个窗口中。所以在.h

            @interface TwoImagesAppDelegate : NSObject <UIApplicationDelegate> {
                UIWindow *window;
                UIViewController* VC;
                UIImageView* imgView;
            }
        

        .m

            - (void) addOneMore
            {
                VC.view.backgroundColor = [UIColor blackColor];
                [imgView setImage:[UIImage imageNamed:@"2.png"]];
                [VC.view addSubview:imgView];   
                [window addSubview:VC.view];
            }
        
            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        
                // Override point for customization after application launch
        
                [window makeKeyAndVisible];
        
                VC = [[UIViewController alloc]init];
                VC.view.backgroundColor = [UIColor redColor];
                imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
                [imgView setImage:[UIImage imageNamed:@"1.png"]];
                [VC.view addSubview:imgView];
                [window addSubview:VC.view];
        
                [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(addOneMore) userInfo:nil repeats:NO];
        
                return YES;
            }
        

        【讨论】:

        • 我会放在另一个答案中
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多