1

在我的一个应用程序中,我需要将 UIImage 拆分为多个部分。以下是我用来拆分的代码。这里我的问题是我无法通过将图像添加到 UIImageView 来加载图像视图。

- (void)viewDidLoad
{
    UIImage* image = [UIImage imageNamed:@"monalisa.png"];

    NSMutableArray* splitImages = [self splitImageIntoRects:(__bridge CGImageRef)(image)];
    printf("\n count; %d",[splitImages count]);

    CALayer *layer = [splitImages objectAtIndex:5];

    CGImageRef imgRef = (__bridge CGImageRef)(layer.contents);
    UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];


    UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    imageview.image = img;
    imageview.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageview];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (NSMutableArray*)splitImageIntoRects:(CGImageRef)anImage
{
CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));

    NSMutableArray *splitLayers = [NSMutableArray array];

   int kXSlices = 3;
   int 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(anImage, frame);
            layer.contents = (__bridge id)subimage;
            [splitLayers addObject:layer];
        }
    }
    return splitLayers; 
}

输出如下, 在此处输入图像描述

4

2 回答 2

3

尝试这个:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getSplitImagesFromImage:[UIImage imageNamed:@"Image1.png"] withRow:4 withColumn:4];
}

-(NSMutableArray *)getSplitImagesFromImage:(UIImage *)image withRow:(NSInteger)rows withColumn:(NSInteger)columns
{
    NSMutableArray *aMutArrImages = [NSMutableArray array];
    CGSize imageSize = image.size;
    CGFloat xPos = 0.0, yPos = 0.0;
    CGFloat width = imageSize.width/rows;
    CGFloat height = imageSize.height/columns;
    for (int aIntY = 0; aIntY < columns; aIntY++)
    {
        xPos = 0.0;
        for (int aIntX = 0; aIntX < rows; aIntX++) 
        {                
            CGRect rect = CGRectMake(xPos, yPos, width, height);
            CGImageRef cImage = CGImageCreateWithImageInRect([image CGImage],  rect);

            UIImage *aImgRef = [[UIImage alloc] initWithCGImage:cImage];
            UIImageView *aImgView = [[UIImageView alloc] initWithFrame:CGRectMake(aIntX*width, aIntY*height, width, height)];
            [aImgView setImage:aImgRef];
            [aImgView.layer setBorderColor:[[UIColor blackColor] CGColor]];
            [aImgView.layer setBorderWidth:1.0];
            [self.view addSubview:aImgView];

            [aMutArrImages addObject:aImgRef];
            xPos += width;
        }
        yPos += height;
    }
    return aMutArrImages;
}

有关更多信息,请参阅此内容,您也可以从此处下载演示。

于 2014-09-02T06:00:02.710 回答
0

我们可以进一步增强 Yasika Patel 答案。下面的功能将为您提供适合您视图的精确图像。

- (void)splitImage :(UIImage *)image withColums:(int)columns WithRows:  (int)rows ViewToIntegrate : (UIView *)view
 {
     CGSize imageSize = _imgSplit.image.size;
     CGFloat xPos = 0.0, yPos = 0.0;
     CGFloat width = imageSize.width/rows;
     CGFloat height = imageSize.height/columns;
     for (int aIntY = 0; aIntY < columns; aIntY++)
      {
        xPos = 0.0;
        for (int aIntX = 0; aIntX < rows; aIntX++)
        {
            CGRect rect = CGRectMake(xPos, yPos, width, height);
            CGImageRef cImage = CGImageCreateWithImageInRect([ image CGImage],  rect);
        
            UIImage *aImgRef = [[UIImage alloc] initWithCGImage:cImage];
            UIImageView *aImgView = [[UIImageView alloc] initWithFrame:CGRectMake(aIntX*(view.frame.size.width/rows), aIntY*( view.frame.size.height/columns), view.frame.size.width/rows, view.frame.size.height/columns)];
        
            [aImgView setImage:aImgRef];
            [aImgView.layer setBorderColor:[[UIColor blackColor] CGColor]];
            [aImgView.layer setBorderWidth:0.5];
            [view addSubview:aImgView];
            xPos += width;
        }
        yPos += height;
     }
  
   [self.view addSubview:view];
}

这将为您提供 9parts 中的图像。在这里你只需要传递行和列。

在此处输入图像描述

于 2016-06-23T07:03:01.707 回答