首頁>Program>source

我需要以程式設計方式將圖像分成9个部分.有關如何執行此操作的任何建議?

最新回復
  • 5月前
    1 #

    下面的代碼也是一種解決方案,它可以檢測出被點击的圖片.這个想法是采用UIImage並使用CGImageCreatewithImageInRect裁剪出片段.从裁剪的片段中建立一个新的UIImage並將其放置在UIImageView中.為了使點击手势起作用,我不得不將UIImageView放在UIView中.最後,提供手势和唯一標簽,以便在敲击時可以識別出棋子。

    - (void)loadView {
        UIView* root = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        UIImage* whole = [UIImage imageNamed:@"whole.jpg"]; //I know this image is 300x300
        int partId = 0;
        for (int x=0; x<=200; x+=100) {
            for(int y=0; y<=200; y+=100) {
                CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
                UIImage* part = [UIImage imageWithCGImage:cgImg];
                UIImageView* iv = [[UIImageView alloc] initWithImage:part];
                UIView* sView = [[UIView alloc] initWithFrame:CGRectMake(200-x, 200-y, 100, 100)];
                [sView addSubview:iv];
                [iv release];
                UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(tap:)];
                tap.numberOfTapsRequired = 1;
                [sView addGestureRecognizer:tap];
                [tap release];
                sView.tag = partId;
                [root addSubview:sView];
                [sView release];
                partId++;
                CGImageRelease(cgImg);
            }
        }
        self.view = root;
    }
    - (void)tap:(UITapGestureRecognizer*)gesture
    {
        NSLog(@"image tap=%d", gesture.view.tag);
    }
    

  • 5月前
    2 #

    有很多方法可以對圖像进行切片和切塊,但這是一種.它使用Quartz將圖像切成9个相等大小的部分.請註意,它不處理旋轉的圖像(因為我的意思是具有imageOrientation!= 0的圖像),但它應该可以帮助您入門:

    +(NSArray *)splitImageInTo9:(UIImage *)im{
        CGSize size = [im size];
        NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:9];
        for (int i=0;i<3;i++){
            for (int j=0;j<3;j++){
                CGRect portion = CGRectMake(i * size.width/3.0, j * size.height/3.0, size.width/3.0, size.height/3.0);
                UIGraphicsBeginImageContext(portion.size);
                CGContextRef context = UIGraphicsGetCurrentContext();
                CGContextScaleCTM(context, 1.0, -1.0);
                CGContextTranslateCTM(context, 0, -portion.size.height);
                CGContextTranslateCTM(context, -portion.origin.x, -portion.origin.y);
                CGContextDrawImage(context,CGRectMake(0.0, 0.0,size.width,  size.height), im.CGImage);
                [arr addObject:UIGraphicsGetImageFromCurrentImageContext()];
                UIGraphicsEndImageContext();
    
            }
        }
        return [arr autorelease];
    }
    

    輸出將是9个圖像的陣列,每个圖像的大小(具有/ 3,高度/ 3)

  • 5月前
    3 #

    以下是基於引數的代碼切片圖像,添加邊框並顯示:

    -(NSMutableArray *)getImagesFromImage:(UIImage *)image withRow:(NSInteger)rows           withColumn:(NSInteger)columns{
      NSMutableArray *images = [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 y = 0; y < columns; y++) {
        xPos = 0.0;
        for (int x = 0; x < rows; x++) {
            CGRect rect = CGRectMake(xPos, yPos, width, height);
            CGImageRef cImage = CGImageCreateWithImageInRect([image CGImage],  rect);
            UIImage *dImage = [[UIImage alloc] initWithCGImage:cImage];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x*width, y*height, width, height)];
            [imageView setImage:dImage];
            [imageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
            [imageView.layer setBorderWidth:1.0];
            [self.view addSubview:imageView];
            [images addObject:dImage];
            xPos += width;
        }
        yPos += height;
    }
    return images;
    }
    

    專案下載鏈接:https://github.com/bpolat/Image-Slicer

    示例用法和結果:

    [self getImagesFromImage:[UIImage imageNamed:@" 1.png"] withRow:4 withColumn:4];

  • 5月前
    4 #

    如果您希望圖像的一部分 要組织起来,您需要在最终视圖中使用一些UIImageView。.只需看一下這段代碼:

       UIImage* whole = [UIImage imageNamed:@"permanent_cosmetics_pretty.jpg"];
        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:
                    self.part1.image=iv.image;
                    break;
                case 1:
                    self.part2.image=iv.image;
                    break;
                case 2:
                    self.part3.image=iv.image;
                    break;
                case 3:
                    self.part4.image=iv.image;
                    break;
                case 4:
                    self.part5.image=iv.image;
                    break;
                case 5:
                    self.part6.image=iv.image;
                    break;
                case 6:
                    self.part7.image=iv.image;
                    break;
                case 7:
                    self.part8.image=iv.image;
                    break;
                case 8:
                    self.part9.image=iv.image;
                    break;
                case 9:
                    self.part10.image=iv.image;
                    break;
                case 10:
                    self.part11.image=iv.image;
                    break;
                case 11:
                    self.part12.image=iv.image;
                    break;
                default:
                    break;
            }               
            [iv release];               
            partId++;
            NSLog(@"part id = %d",partId);
            }
       }
      [self.view addSubview:self.finalView];   
    

相似問題

  • performance:傳遞大量資料的查詢的最佳MySQL設置?
  • buffer:PHP Flush / ob_flush無法正常工作