1

私のアプリケーションの 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 Answerをさらに強化できます。以下の関数は、ビューに適合する正確な画像を提供します。

- (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 に答える