开发者

Problem in Rendering images in UIImageView in iPhone programming

I've a UIViewController and a UIImageView, on UIImageView i want to flip between 2 images, which i'm not able to achieve.

This is the code i've written, plz correct me if i'm wrong.

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 setIm开发者_运维知识库age:[UIImage imageNamed:@"Image_B.jpg"]];
[VC.view addSubview:imgView];

[window addSubview:VC.view];

so when i execute this project, only Image_B is displayed on screen, while i want Image_A to be displayed then on sleep(2), Image_B has to be displayed.

How would i make it possible??

Thank You.


Instead of sleep use an NSTimer (docs here). The timer does not block the thread, but instead returns to the run loop. This way UIKit can submit changes in the view hierarchy and display them on screen. In the callback of the timer you just change the image.


.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;
    }

please note that this example show how to use timer, there is memory leak.


I assume you are in a window. So in .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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜