【问题标题】:UIimageView array of images fade from one image to the next bugUIimageView 图像数组从一个图像淡入到下一个错误
【发布时间】:2012-07-20 12:25:00
【问题描述】:

您好,我有一些最有效的代码。它基本上以淡入淡出的效果对数组中的图像数量进行动画处理。

基本上一切正常,但是我有一个小错误,当视图第一次加载 第一个图像时,数组中似乎旋转了 90 度到正确的位置然后淡出到相同的图像,但看到这样的动画很烦人。所有从图像到另一个图像的褪色都是完美的,只是上面提到的错误。如前所述,仅在首次加载时

这里是代码。 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];

【问题讨论】:

  • 您从哪里加载实际图像?这些 PNG 在您的捆绑包中吗?请添加执行此操作的相关代码。
  • @stavash 嗨,对不起,我应该提到这些图像是从 imagePicker(相机)保存到文档目录的,并且图像被保存为 JPGS,以在拍摄图像时保持正确的方向.然后从 docs 目录加载它们并添加到上面提到的名为 images 的可变数组中。
  • 有人知道一张图片发生了什么吗?

标签: ios uiimageview core-animation nstimer nsrunloop


【解决方案1】:

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

【讨论】:

  • 感谢您的回答 stavash 作为测试我已经尝试了应用程序包中的其他一些图像,它仍然可以这样做。有趣的是,如果我交换这一行 [imageViewTop setImage:[images objectAtIndex:topIndex]]; for [imageViewBottom setImage:[images objectAtIndex:topIndex]];然后旋转另一张图片。
  • 嗯...有趣。在 Interface Builder 中定义这些图像视图怎么样?这样您就可以确保它们已正确配置和定位。
猜你喜欢
  • 1970-01-01
  • 2014-08-22
  • 2013-07-15
  • 2013-07-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
相关资源
最近更新 更多