一段簡單的圖片裁剪代碼

        UIImage *srcimg = [UIImage imageNamed:@"test.png"];//test.png寬172 高192

        UIImageView *imgview = [[UIImageView alloc] init];        
        imgview.frame = CGRectMake(10, 150, 300, 220);

        //要裁剪的圖片區域,按照原圖的像素大小來,超過原圖大小的邊自動適配
        CGRect rect =  CGRectMake(0, 0, 300, 100);       
        CGImageRef cgimg = CGImageCreateWithImageInRect([srcimg CGImage], rect);        
        imgview.image = [UIImage imageWithCGImage:cgimg];        
        CGImageRelease(cgimg);//用完一定要釋放,否則內存泄露        
        [self.view addSubview:imgview];
發佈了71 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章