2014-09-01 142 views
1

在我的一個應用程序中,我需要將UIImage拆分爲多個部分。以下是我用來拆分的代碼。這裏我的問題是我無法通過將圖像添加到UIImageView來加載圖像視圖。IOS:如何將UIImage拆分爲部分

- (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; 
} 

,輸出爲像如下, enter image description here

回答

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; 
} 

更多信息see this,你也可以從here下載試玩。

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]; 
} 

這會給你在9個部分的圖像。在這裏你只需要通過該行和colums。

enter image description here