iOS 9宫格切割图像

作者: Sandy苗 | 来源:发表于2017-06-06 10:59 被阅读787次

    实现效果前


    屏幕快照 2017-06-05 下午5.32.59.png

    实现效果后


    屏幕快照 2017-06-05 下午5.33.13.png
    一.首先要做如果切割图片,切割图片的代码如下:
    - (UIImage *)captureView:(UIView *)theView frame:(CGRect)fra {
    
            UIGraphicsBeginImageContext(theView.frame.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            [theView.layer renderInContext:context];
            UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, fra);
            UIImage *i = [UIImage imageWithCGImage:ref];
            CGImageRelease(ref);
            return i;
    
    }
    

    二.点击切图前,用UIImageView把图片加载出来(切割前的效果):

    - (void)viewDidLoad {
    
            [super viewDidLoad];
            UIImage *image = [UIImage imageNamed:@"image"];
            _bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 100, 315,315/image.size.width*image.size.height )];
            _bigImageView.image = image;
            [self.view addSubview:_bigImageView];
    
    }
    

    三.点击切图:
    把图片切成九宫格,就是横着切3段,竖着切3段,2个for循环,对已知image进行切割。切割的rect:CGRectMake(jself.bigImageView.frame.size.width1.0/3,iself.bigImageView.frame.size.height1.0/3,self.bigImageView.frame.size.width1.0/3,self.bigImageView.frame.size.height1.0/3)不要搞错,获得9张图片的数组。

    - (void)captureImageArr {
            self.imageArr = [[NSMutableArray alloc]init];
            for (int i = 0; i < 3; i++) {
                    for (int j = 0; j<3; j++) {
                            UIImage *theImage = [self captureView:self.bigImageView     frame:CGRectMake(j*self.bigImageView.frame.size.width*1.0/3,i*self.bigImageView.frame.size.height*1.0/3,self.bigImageView.frame.size.width*1.0/3,self.bigImageView.frame.size.height*1.0/3)];
                            [self.imageArr addObject:theImage];
                    }
             }
    }
    
    

    四.将获得的数组用UICollectionView接收,组成9宫格

    五.完整代码如下

    static NSString *const cellId = @"cellId";
    
    @interface ViewController ()
    
    @property (nonatomic,strong)UICollectionView *collectionView;
    
    @property (nonatomic,strong) UIImageView *bigImageView;
    
    @property (nonatomic,strong) NSMutableArray *imageArr;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {    
    
    [super viewDidLoad]; 
    
    UIImage *image = [UIImage imageNamed:@"image"];   
    
     _bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 100, 315,315/image.size.width*image.size.height )];    
    
    _bigImageView.image = image;    [self.view addSubview:_bigImageView];
    
    }
    
    - (void)captureImageArr {    
    
    self.imageArr = [[NSMutableArray alloc]init];   
    
     for (int i = 0; i < 3; i++) {        
    
          for (int j = 0; j<3; j++) {            
    
    UIImage *theImage = [self captureView:self.bigImageView frame:CGRectMake(j*self.bigImageView.frame.size.width*1.0/3,i*self.bigImageView.frame.size.height*1.0/3,self.bigImageView.frame.size.width*1.0/3,self.bigImageView.frame.size.height*1.0/3)];                     
    
    [self.imageArr addObject:theImage];            
    
      }    }}
    
    - (UIImage *)captureView:(UIView *)theView frame:(CGRect)fra {    
    
    UIGraphicsBeginImageContext(theView.frame.size);    
    
    CGContextRef context = UIGraphicsGetCurrentContext();    
    
    [theView.layer renderInContext:context];    
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();   
    
     UIGraphicsEndImageContext();    
    
    CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, fra);    
    
    UIImage *i = [UIImage imageWithCGImage:ref];   
    
     CGImageRelease(ref);    
    
    return i;                            
    
    }
    
    - (IBAction)CutAction:(id)sender {
    
    [self captureImageArr];
    
    //此处必须要有创见一个UICollectionViewFlowLayout的对象
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    
    //同一行相邻两个cell的最小间距
    
    layout.minimumInteritemSpacing = 5;
    
    //最小两行之间的间距
    
    layout.minimumLineSpacing = 5;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:_bigImageView.frame collectionViewLayout:layout];
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    _collectionView.dataSource = self;
    
    _collectionView.delegate = self;
    
    _collectionView.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:_collectionView];
    
    }
    
    #pragma mark ---- UICollectionViewDataSource
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    
    {
    
    return 1;
    
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    
    {
    
    return 9;
    
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
    UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.contentView.frame];
    
    imageView.image = self.imageArr[indexPath.row];
    
    [cell.contentView addSubview:imageView];
    
    return cell;
    
    }
    
    //定义每一个cell的大小
    
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
    return CGSizeMake((self.bigImageView.frame.size.width-10)/3, (self.bigImageView.frame.size.height-10)/3);
    
    }
    
    //cell的点击事件
    
    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //cell被点击后移动的动画
    
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
    
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:iOS 9宫格切割图像

        本文链接:https://www.haomeiwen.com/subject/cjjgfxtx.html