程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-iphone關於圖片的剪裁問題

ios-iphone關於圖片的剪裁問題

編輯:編程綜合問答
iphone關於圖片的剪裁問題

用下面的代碼剪裁圖片:

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
 {
     CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
     // or use the UIImage wherever you like
     UIImage * img =  [UIImage imageWithCGImage:imageRef];
     CGImageRelease(imageRef);
     return img;
 }

在模擬器中運行的好好的,但是到了設備上就出問題了。

最佳回答:


創建一個UIImage:

@implementation UIImage (Crop)

- (UIImage *)crop:(CGRect)cropRect {

    cropRect = CGRectMake(cropRect.origin.x*self.scale, 
                      cropRect.origin.y*self.scale, 
                      cropRect.size.width*self.scale, 
                      cropRect.size.height*self.scale);       

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
    UIImage *result = [UIImage imageWithCGImage:imageRef 
                                          scale:self.scale 
                                    orientation:self.imageOrientation]; 
    CGImageRelease(imageRef);
    return result;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved