2014-02-10 4 views
0

단추를 누를 때 UIImageView 하나의 이미지를 반복하려고합니다. 버튼을 누른 후 0.1 초 후에 이미지가 사라져야합니다. 다음은 코드입니다 :UIImageView에서 이미지로드 지연

int tapCount = 0; 

UIImage *image0 = [UIImage imageNamed:@"0.jpg"]; 
UIImage *image1 = [UIImage imageNamed:@"1.jpg"]; 
UIImage *image2 = [UIImage imageNamed:@"2.jpg"]; 
imagesArray = [[NSArray alloc] initWithObjects:image0, image1, image2, nil]; 


-(IBAction)backgroundButton:(id)sender{ 

    self.myImageView.image = [imagesArray objectAtIndex:tapCount%3]; 
    tapCount++; 
    [self performSelector:@selector(eraseImage) withObject:self afterDelay:0.1]; 
} 

-(void)eraseImage{ 

    self.myImageView.image = nil; 
} 

문제는 내가 (4 탭에서) 하나 개의 전체 루프를 완료 할 때까지 이미지가 표시되지 않는 것입니다. 나는 여하튼 도청과 이미지가 나타나는 사이에 약간의 시간이 걸리고, 0.1 초 후에 사라지기 때문에 어쨌든 나는 UIImageView의 이미지를 초기화해야한다고 생각합니다. 전혀 보이지 않습니다. 나는이 같은 viewDidLoad 내부를로드하려고했습니다

:

for(int i = 0; i<[imagesArray count]; i++){ 
    self.myImageView.image = [imagesArray objectAtIndex : i]; 
} 

그러나 그것은 단지 마지막 이미지와 함께 작동하는 부하 (이 경우 이미지 2).

다른 루프 내에서 UIImageView 대신 UIImage을 반복하면 UIImageView 하나가됩니다.

다른 힌트가 있습니까?

+0

이미지의 크기 및 크기는 얼마나됩니까? 'UIImage'를 만드는 것은 실제로 이미지 데이터를로드하지 않습니다 (당신은 그것을 발생시키기 위해 컨텍스트에 렌더링해야합니다). – Wain

+0

이미지는 1MB에서 3MB 사이의 jpg입니다. 1024x768은 iPad에서 전체 화면이어야하기 때문에. 전에 어떻게로드 할 수 있습니까? – guardabrazo

+0

idk 코드가있는 경우이 코드 앞에 =가 없어야합니다. 그것은 [imagesArray objectAtIndex : i]이어야합니다; – user2277872

답변

0

내가 생각하는 애니메이션을 구현하는 데 훨씬 간단한 방법이 있다고 생각합니다. 다음 코드를 시도해보십시오 :

-(IBAction)backgroundButton:(id)sender{ 


    [UIView animateWithDuration:0.2 
         delay:nil 
         options:UIViewAnimationCurveEaseIn 
        animations:^{ 
         self.myImageView.image = [imagesArray objectAtIndex:tapCount%3]; 
         self.myImageView.image = nil; 

         } 
      completion:nil 
    ]; 

    tapCount++; 

    if (tapCount == 2) { 
     tapCount = 0; 
    } 

} 
+0

이 애니메이션을 사용하면 0.1 초 후에 이미지가 사라지지 않습니다. 'completion :^(BOOL finished) { self.myImageView를 추가하려고했습니다.이미지 = 없음; }'그러나이 방법으로 이미지가 나타나지 않습니다. – guardabrazo

+0

그러면 사용자가 0.1 초 동안 이미지를 표시 한 다음 사라지도록 클릭 한 후? – Mika

+0

예! 그게 내가 원하는거야. – guardabrazo

2

UIImage를 만드는 것은 실제로 이미지 데이터를로드하지 않습니다. 따라서 이미지가 크면 화면에 실제로 렌더링되기 전에 숨길 수 있습니다. 동시에 메모리에 많은 이미지를 저장할 수는 없지만 문맥을 만들고 그 안에 이미지를 드로잉하여 이미지 데이터를 강제로로드 할 수 있습니다 (배경에서 수행 할 수 있음, CGContextDrawImage 사용).

this 또는 확인 this discussion과 같은 제 3 자 코드가 있습니다.

0

있는 UIImageView의 animationImagesanimationDuration 특성 :

먼저 나는 백그라운드 스레드에서 모든 이미지를 미리로드를

-(void)preload:(UIImage *)image{ 

CGImageRef ref = image.CGImage; 
size_t width = CGImageGetWidth(ref); 
size_t height = CGImageGetHeight(ref); 
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, space, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst); 
CGColorSpaceRelease(space); 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), ref); 
CGContextRelease(context); 
} 

그런 다음 처음부터 내가했던 것과 동일한 조치를 취합니다.

-(IBAction)backgroundButton:(id)sender{ 

    self.myImageView.image = [imagesArray objectAtIndex:tapCount%3]; 
    tapCount++; 
    [self performSelector:@selector(eraseImage) withObject:self afterDelay:0.1]; 
} 

-(void)eraseImage{ 

    self.myImageView.image = nil; 
} 
관련 문제