UIimageView图像数组从一个图像淡入下一个bug

时间:2012-07-20 12:25:01

标签: ios uiimageview core-animation nstimer nsrunloop

嗨,我有一些主要有效的代码。它基本上是动画,具有淡入淡出效果,来自阵列的图像数量。

基本上一切都很好但是我有一个轻微的错误,当视图首次加载时,数组中的第一个图像似乎旋转90度到正确的位置然后逐渐消失到相同的图像,但看起来像这样的动画很烦人。所有从图像到另一个的淡出都是完美的,只是上面提到的bug。如上所述只是在首次加载时

这是代码。 imagesMutableArraytopImageViewbottomImageView都是 ivars 和**合成**。 images数组也是如此。

int topIndex = 0, bottomIndex = 1;
-(void)imageFader{

self.imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];
[self.view addSubview:imageViewBottom];
[imageViewBottom setAlpha:0.0];
[imageViewBottom release];

self.imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];
[self.view addSubview:imageViewTop];
[imageViewTop setAlpha:1.0];
[imageViewTop release];


    [imageViewTop setImage:[images objectAtIndex:topIndex]];


    timer = [NSTimer timerWithTimeInterval:1.0 
                                             target:self 
                                           selector:@selector(onTimer) 
                                           userInfo:nil 
                                            repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [timer fire];


}

-(void)onTimer{

[UIView animateWithDuration:1 animations:^{

            //set the image of the image that is not currently visible

    if (imageViewTop.alpha == 0) {
        [imageViewTop setImage:[images objectAtIndex:topIndex]];

    }
    if (imageViewBottom.alpha == 0) {
        [imageViewBottom setImage:[images objectAtIndex:bottomIndex]];

    }

    //make sure the images rotate
    [imageViewTop setAlpha:imageViewTop.alpha == 0 ? 1 : 0];
    [imageViewBottom setAlpha:imageViewBottom.alpha == 0 ? 1 : 0];

    //make sure the images play in a loop

    topIndex = topIndex < [images count]-1 ? bottomIndex+1 : 0;
    bottomIndex = bottomIndex < [images count]-1 ? bottomIndex+1 : 0;}];



}

以下是图像在加载到动画的MutableArray之前如何保存到docs目录。

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

{

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
        NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];


        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        //Save to camera roll       
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) ALAssetOrientationRight completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"completionBlock");

        }];


        int imageNumber = 0;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *pathToFile;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];


        do
        {
            // increment the image
            imageNumber++;

            // get the new path to the file
            pathToFile = [documentsDirectory stringByAppendingPathComponent:
                          [NSString stringWithFormat:
                           @"standingImage%d.JPG", imageNumber]];
        }
        while([fileManager fileExistsAtPath:pathToFile]);
        /* so, we loop for as long as we keep coming up with names that already exist */

        UIImage *image2 = image; // imageView is my image from camera

        //Use UIImageJPEGRepresentation to maitain image orientation!
        NSData *imageData = UIImageJPEGRepresentation(image2, 0.9);
        [imageData writeToFile:pathToFile atomically:NO];



        [self dismissModalViewControllerAnimated:YES];

1 个答案:

答案 0 :(得分:0)

这种意外的方向可能是由图像的起源--UIImagePickerController引起的。尝试访问您在评论中提到的JPEG文件并检查其方向,然后您至少会缩小问题的潜在原因。