2012-12-09 28 views
4

我想使用此代碼,允許用戶縮放和裁切自己的形象在我的應用程序: https://github.com/iosdeveloper/ImageCropper/tree/master/Classes/ImageCropper使用縮放的Objective-C iOS裁剪圖像?

代碼片段

float zoomScale = 1.0/[scrollView zoomScale]; 

CGRect rect; 
rect.origin.x = [scrollView contentOffset].x * zoomScale; 
rect.origin.y = [scrollView contentOffset].y * zoomScale; 
rect.size.width = [scrollView bounds].size.width * zoomScale; 
rect.size.height = [scrollView bounds].size.height * zoomScale; 

CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect); 

UIImage *cropped = [UIImage imageWithCGImage:cr]; 

CGImageRelease(cr); 

[delegate imageCropper:self didFinishCroppingWithImage:cropped]; 

但它沒有考慮到正在旋轉的帳戶中的圖片(我猜它是iPhone圖像上的某種元數據)。我不想讓用戶畫一個矩形來裁剪,因爲我更喜歡這個界面。

有人能告訴我如何使它不旋轉我的圖像?

我正在建設iOS 5 & 6如果有幫助。

[編輯]另外,這在Game Center應用程序中很好地完成,用於上傳您的個人資料圖片。任何人都知道我在哪裏可以找到代碼?

回答

3

我發現這裏的答案,幫助我做什麼,我想 iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay

代碼段爲那些有興趣

float zoomScale = 1.0/[scrollView zoomScale]; 

CGRect rect; 
NSLog(@"contentOffset is :%f,%f",[scrollView contentOffset].x,[scrollView contentOffset].y); 
rect.origin.x = fabsf([scrollView contentOffset].x * zoomScale); 
rect.origin.y = fabsf([scrollView contentOffset].y * zoomScale); 
rect.size.width = fabsf([scrollView bounds].size.width * zoomScale); 
rect.size.height = fabsf([scrollView bounds].size.height * zoomScale); 

UIGraphicsBeginImageContextWithOptions(CGSizeMake(rect.size.width, 
                rect.size.height), 
             NO, 
             0.); 
[[imageView image] drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y) 
      blendMode:kCGBlendModeCopy 
       alpha:1.]; 
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+1

現在,這是造成大量的內存使用..有一個更好的解決方案? –