1

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

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

这是代码。imagesis an MutableArrayand both topImageViewand bottomImageViewboth is an ivars and **综合**。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];
4

1 回答 1

0

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

于 2012-07-20T13:39:29.137 回答