2011-11-09 59 views
4

我有一段時間使用CGImageCreateWithImageInRect裁剪照片。我的應用程序讓用戶移動/放大圖像,直到它填充316 x 316視圖,然後我希望它裁剪框外的任何區域並將圖像保存爲UIImage。我通過獲取x和y圖像視圖原點的絕對值來確定作物的原點,因爲這將我帶回到包含圖像視圖的視圖/框的0,0。下面是代碼:CGImageCreateWithImageInRect不是正確裁剪

CGRect croppedRect = CGRectMake(fabs(widthDistanceToOrigin), fabs(HeightDistanceToOrigin), 316, 316); 
    CGImageRef croppedImageRef = CGImageCreateWithImageInRect(image.CGImage, croppedRect); 
    UIImage *croppedImage = [UIImage imageWithCGImage: croppedImageRef scale: 1/(imageScale) orientation: image.imageOrientation]; 
    CGImageRelease(croppedImageRef); 

這讓我瘋狂,我不能讓它收割我想要的區域。我已經把它正確地縮放了,但是問題似乎與作物矩形的x和y原點有關,它離它應該採取的區域有點偏離(有時我會得到奇怪的結果)。

此外,它似乎與手機相機拍攝的圖像,我必須乘以我的作物rect一切爲2,因爲它是正確的大小。很奇怪。

所以我想知道,有沒有人知道如何解決這個問題,或者有沒有人可能有更好的裁剪方式(請記住我需要裁剪照片中的一個區域)。謝謝!

+0

請在答案中提供您的解決方案,並在系統允許時接受。 –

回答

0

我發現了這個問題。我在縮放圖像之前試圖抓取裁剪區域。現在必須弄清楚如何在「CGImageCreateWithImageInRect」之前減少圖像。

4

有關於這個話題這麼多的帖子與他們具有相同的確切答案, 利用:CGImageCreateWithImageInRect 但它們都沒有完全解決什麼是試圖要裁剪小姐的定位的問題。

但是,只有一個線程(深埋在內心深處),有進去 CGImageCreateWithImageInRect(image.CGImage,croppedRect一個小細節,每個人都缺少... How to crop a UIImageView to a new UIImage in 'aspect fit' mode?

的「矩形」參數) 需要表示(取自)UIImage,而不是UIImageView。

這應該解決在座標系中未命中定位的問題。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info 
{ 
     oImage = [info valueForKey:UIImagePickerControllerOriginalImage]; 
     NSLog(@"Original Image size width: %f x height: %f", oImage.size.width, oImage.size.height); 


     // crop image according to what the image picker view is displaying 
     CGRect rect = CGRectMake(0, 40, 960.0f, 960.0f); // note: 960 x 1280 is what natively comes from capturing and image 
     nImage = [BGViewModifiers cropImage:oImage inRect:rect]; 

     // scale image down for display and creating kombie 
     CGSize size = CGSizeMake(320.0f, 320.0f); 
     nImage = [BGViewModifiers imageFromImage:nImage scaledToSize:size]; 

     NSLog(@"New Image size width: %f x height: %f", [nImage size].width, [nImage size].height); 
} 

//ref: http://stackoverflow.com/a/25293588/2298002 
+ (UIImage *)cropImage:(UIImage*)image inRect:(CGRect)rect 
{ 
    double (^rad)(double) = ^(double deg) { 
     return deg/180.0 * M_PI; 
    }; 

    CGAffineTransform rectTransform; 
    switch (image.imageOrientation) { 
     case UIImageOrientationLeft: 
      rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height); 
      break; 
     case UIImageOrientationRight: 
      rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0); 
      break; 
     case UIImageOrientationDown: 
      rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height); 
      break; 
     default: 
      rectTransform = CGAffineTransformIdentity; 
    }; 
    rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale); 

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectApplyAffineTransform(rect, rectTransform)); 
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; 
    CGImageRelease(imageRef); 

    return result; 
} 

+ (UIImage*)imageFromImage:(UIImage*)image scaledToSize:(CGSize)newSize 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 
2

我想這基於我自己的經驗提供更多信息/答案。

UIImage(從而CGImageCreateWithImageInRect:)角度來看,(0, 0)位於左下方。在確定作物廣場,你可以簡單地計算基於標準縱向方形,然後使用@溫室的回答方法(貼在這裏再次清楚和改用CGFloat來修復鑄件的錯誤。

- (UIImage *)cropImage:(UIImage*)image toRect:(CGRect)rect { 
    CGFloat (^rad)(CGFloat) = ^CGFloat(CGFloat deg) { 
     return deg/180.0f * (CGFloat) M_PI; 
    }; 

    // determine the orientation of the image and apply a transformation to the crop rectangle to shift it to the correct position 
    CGAffineTransform rectTransform; 
    switch (image.imageOrientation) { 
     case UIImageOrientationLeft: 
      rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height); 
      break; 
     case UIImageOrientationRight: 
      rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0); 
      break; 
     case UIImageOrientationDown: 
      rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height); 
      break; 
     default: 
      rectTransform = CGAffineTransformIdentity; 
    }; 

    // adjust the transformation scale based on the image scale 
    rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale); 

    // apply the transformation to the rect to create a new, shifted rect 
    CGRect transformedCropSquare = CGRectApplyAffineTransform(rect, rectTransform); 
    // use the rect to crop the image 
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, transformedCropSquare); 
    // create a new UIImage and set the scale and orientation appropriately 
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; 
    // memory cleanup 
    CGImageRelease(imageRef); 

    return result; 
} 

此代碼將裁切方塊移動到正確的相對位置

+0

完美適合我 –