【问题标题】:Slice multiple part from single image in iPhone? [duplicate]从 iPhone 中的单个图像中分割多个部分? [复制]
【发布时间】:2013-04-16 15:53:33
【问题描述】:

我在 iPhone 应用程序中工作,我尝试从单个图像中分割多个部分,但我无法获得确切的解决方案。我想要单个图像切片多个部分,如 3x3、4x4、5x5、6x6 等......怎么做?是否有可能做到这一点?请帮帮我。

提前致谢

我试过了:

UIImage* whole = [UIImage imageNamed:@"1.png"];
    int partId = 0;
    for (int x=0; x<=300; x+=100)
    {
        for(int y=0; y<=300; y+=100)
        {

            CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
            UIImage* part = [UIImage imageWithCGImage:cgImg];
            UIImageView* iv = [[UIImageView alloc] initWithImage:part];

            switch (partId)
            {
                case 0:
                    img.image=iv.image;
                    break;
                case 1:
                    img2.image=iv.image;
                    break;
                case 2:
                    img3.image=iv.image;
                    break;
                case 3:
                    img4.image=iv.image;
                    break;
                case 4:
                    img5.image=iv.image;
                    break;
                case 5:
                    img6.image=iv.image;
                    break;
                case 6:
                    img7.image=iv.image;
                    break;
                case 7:
                    img8.image=iv.image;
                    break;
                case 8:
                    img9.image=iv.image;
                    break;
                default:
                    break;
            }

            partId++;
            NSLog(@"part id = %d",partId);
        }
    }

【问题讨论】:

  • 请显示您当前拥有的代码并告诉我们它有什么问题。
  • 感谢您的回复
  • 请看上面,现在我更新了。

标签: iphone uiimageview uiimage crop slice


【解决方案1】:

试试这个:

- (NSArray*)splitImageIntoRects:(CGImageRef)anImage
{
    CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));

    NSMutableArray *splitLayers = [NSMutableArray array];

    kXSlices = 3;
    kYSlices = 3;        

    for(int x = 0;x < kXSlices;x++) {
        for(int y = 0;y < kYSlices;y++) {
            CGRect frame = CGRectMake((imageSize.width / kXSlices) * x,
                                 (imageSize.height / kYSlices) * y,
                                 (imageSize.width / kXSlices),
                                 (imageSize.height / kYSlices));

            CALayer *layer = [CALayer layer];
            layer.frame = frame;                                                    
            CGImageRef subimage = CGImageCreateWithImageInRect(drawnImage, frame);
            layer.contents = (id)subimage;
            CFRelease(subimage);
            [splitLayers addObject:layer];
        }
    }
    return splitLayers; 
}

注意:获取 CGImageRef:

CGImageRef anImage = [myUIImage CGImage];

将 CGImageRef 转换为 UIImage

CALayer *layer = [splitLayers objectAtIndex:0];
CGImageRef imgRef = layer.contents;
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
ImageView1.image = img;

【讨论】:

  • 感谢您的回复
  • 如何将分割后的图片添加到uiimage
  • ImageView1.image = [UIImage imageNamed:[splitLayers objectAtIndex:0]]; // 错误来了 libc++abi.dylib: terminate called throwing an exception
  • 您不能将 CALayer 添加到图像视图。而是使用 '[[splitLayers objectAtIndex:0] content]'。
猜你喜欢
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
相关资源
最近更新 更多