图片拼接成动画瓷砖 - iOS设备(Image Spliced into tiles with a

2019-07-31 09:17发布

我试图拼接图像,然后有下一个像这样的转变? 从哪里开始从任何建议? 是否有可能将图像拼接成的小地砖,并与UIView的动画制作动画呢? 这里是一个样本 。

编辑:

 '#define DESIRED_WIDTH 40

 '#define DESIRED_HEIGHT 60


 - (void)viewDidLoad
{
[super viewDidLoad];

  UIImage *bigImage = [UIImage imageNamed:@"background_for_Manager_App.png"];
  CGRect frame = CGRectMake(0.0, 0.0, DESIRED_WIDTH, DESIRED_HEIGHT);
  int widthCount = (bigImage.size.width / DESIRED_WIDTH);
  int heightCount = (bigImage.size.height / DESIRED_HEIGHT);
  NSLog(@"height width %i %i",heightCount,widthCount);
  for (int i = 0; i <heightCount; i++) {
    for (int j = 0; j < widthCount; j++) {
        UIImage *piece = [self imageCroppedWithRect:frame];
        UIImageView *view = [[UIImageView alloc] initWithImage:piece];
        view.frame = frame;
        [self.view addSubview:view];

        frame.origin.x += frame.size.width;
        NSLog(@"x value %f",frame.origin.x);

       }
    frame.origin.x = 0.0;

    frame.origin.y += frame.size.height;
     NSLog(@"y value %f",frame.origin.y);
   }

//[UIView beginAnimations:nil context:12    // Do any additional setup after loading the view, typically from a nib.

}

- (UIImage *)imageCroppedWithRect:(CGRect)rect
  {
    CGFloat scale = [UIImage imageNamed:@"background_for_Manager_App.png"].scale;
    NSLog(@"scale value%f",scale);
    if (scale > 1.0f) 
    {    // this is for Retina display capability
         rect = CGRectMake(rect.origin.x * scale,
                      rect.origin.y * scale,
                      rect.size.width * scale,
                      rect.size.height * scale);

    }

    CGImageRef imageRef = CGImageCreateWithImageInRect([UIImage imageNamed:@"background_for_Manager_App.png"].CGImage, rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:scale orientation:self.splicedImageView.image.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}

Answer 1:

使用作物这段代码(你最好将其添加为UIImage类别):

- (UIImage *)imageCroppedWithRect:(CGRect)rect
{
    if (self.scale > 1.0f) {    // this is for Retina display capability
        rect = CGRectMake(rect.origin.x * self.scale,
                          rect.origin.y * self.scale,
                          rect.size.width * self.scale,
                          rect.size.height * self.scale);
    }

    CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}

使用方法:您应该创建一个类别UIImage与此代码。 然后加载图像,并分析它inot件:

UIImage *bigImage = [UIImage imageNamed:@"big_image"];
CGRect frame = CGRectMake(0.0, 0.0, DESIRED_WIDTH, DESIRED_HEIGHT);
for (int i = 0; i < bigImage.size.height / DESIRED_HEIGHT; i++) {
    for (int j = 0; i < bigImage.size.width / DESIRED_WIDTH; j++) {
        UIImage *piece = [bigImage imageCroppedWithRect:frame];
        UIImageView *view = [[UIImageView alloc] initWithImage:piece];
        view.frame = frame;
        [self.view addSubview:view];
        [view release];
        frame.origin.x += frame.size.width;
    }
    frame.origin.x = 0.0;
    frame.origin.y += frame.size.height;
}

只是切片图像成片,然后创建适量UIImageView对象并将其放置在正确的顺序,以您的观点。 而现在,你希望你能动画。

还要注意的是DESIRED_WIDTH和DESIRED_HEIGHT必须把你的形象的两侧没有剩余。

对于喜欢动画的视频,你应该使用UIViewAnimationOptionTransitionFlipFromLeft选项:

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
    // hide your image here
} completion:nil];


文章来源: Image Spliced into tiles with animation - iOS
标签: ios xcode splice