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

ios-剪裁圖片導致圖片向左旋轉

編輯:編程綜合問答
剪裁圖片導致圖片向左旋轉

做了一個剪裁圖片的程序,調用剪裁:

UIImage* croppedImage = [self imageCrop:imageView toRect:CGRectMake(10.0, 50.0, 320, 100)];

方法:

{
    //create a context to do our clipping in
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform);
    UIImage *imageToCrop = imageViewToCrop.image;
    UIGraphicsBeginImageContext(newRect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //create a rect with the size we want to crop the image to
    //the X and Y here are zero so we start at the beginning of our
    //newly created context
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    CGContextClipToRect( currentContext, clippedRect);
    //draw the image to our clipped context using our offset rect
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage);
    //pull the image from our cropped context
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return cropped;
}

設置圖片到UIImageview的時候,圖片向左旋轉了。而且我也沒辦法讓他向上旋轉,怎麼辦?

最佳回答:


如樓上所說的可能是imageOrientation屬性的問題。並且你也沒有正確保存scale。這樣創建剪裁的圖片:

CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext);
UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation];
CGImageRelease(croppedRef);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved