2012-07-20 30 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