2012-07-20 31 views
1

嗨,我有一些代码,主要工作。它基本上具有淡入淡出效果,可以从数组中获取图像的数量。UIimageView图像阵列从一个图像淡入淡出到下一个bug

基本上这一切运作良好,但是我有一个轻微错误,当视图第一次加载数组中的第一个图像只似乎90度旋转到正确的位置,然后淡出,以相同的图像,但其讨厌看到这样的动画。所有从图像褪色到另一个完美只是与上述错误。正如刚刚提到的仅在第一次加载时。

这是代码。 imagesMutableArray并且topImageViewbottomImageView都是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的动画被加载之前保存到文档目录。

-(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]; 
+0

你要从哪里下载实际图像?这些PNG是你的包吗?请添加相关的代码。 – Stavash 2012-07-20 12:30:54

+0

@stavash嗨对不起,我应该提到这些图像从imagePicker(相机)保存到文档目录,图像被保存为JPGS以保存图像拍摄时的正确方向。然后从docs目录加载它们,并将其添加到上面提到的名为images的可变数组中。 – 2012-07-20 12:32:39

+0

任何人都有一个想法发生什么与一个图像? – 2012-07-20 13:34:36

回答

0

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

+0

感谢您的答案stavash作为一个测试我已经尝试从应用程序包的一些其他图像,它仍然这样做。有趣的是,如果我交换这一行[imageViewTop setImage:[images objectAtIndex:topIndex]];对于[imageViewBottom setImage:[images objectAtIndex:topIndex]];然后另一个图像被旋转。 – 2012-07-20 13:43:54

+0

嗯......有趣。在Interface Builder中定义这些图像视图怎么样?这样你就可以确保它们被正确配置和定位。 – Stavash 2012-07-20 13:45:01