2014-03-31 57 views
1

我想使用矩形框裁剪圖像。但不知怎的,根據需要無法做到這一點。使用邊框的裁剪圖像

這裏就是我想:

enter image description here

這裏是我想要的結果:

enter image description here

現在我需要的是當單擊完成圖像應在矩形裁剪形狀完全放置在圖像中。我嘗試了一些東西,如使用蒙版圖像矩形屏蔽&繪製圖像,但沒有成功。

這裏是我的代碼是不工作:

CALayer *mask = [CALayer layer]; 
mask.contents = (id)[imgMaskImage.image CGImage]; 
mask.frame = imgMaskImage.frame; 
imgEditedImageView.layer.mask = mask; 
imgEditedImageView.layer.masksToBounds = YES; 

任何人都可以建議我來實現它的更好的方法。

我已經嘗試過很多其他的東西&浪費時間所以請如果我得到一些幫助,它會很好&讚賞。

謝謝。

+0

檢查它http://stackoverflow.com/questions/8252779/crop-an-image-using-rectangular-box-of-selected-area – ChenSmile

+0

@Immi我已經實現的東西與廣場,但爲橢圓形,我想要如上面的屏幕不工作 – Mayur

+0

我沒有得到你... – ChenSmile

回答

1
- (UIImage *)croppedPhoto { 
    // For dealing with Retina displays as well as non-Retina, we need to check 
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect 
    // passed in, and not the size of the view we are taking a screenshot of. 
     CGRect croppingRect = CGRectMake(imgMaskImage.frame.origin.x, 
    imgMaskImage.frame.origin.y, imgMaskImage.frame.size.width, 
    imgMaskImage.frame.size.height); 

    imgMaskImage.hidden=YES; 

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, 
    [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(croppingRect.size); 
    } 

    // Create a graphics context and translate it the view we want to crop so 
    // that even in grabbing (0,0), that origin point now represents the actual 
    // cropping origin desired: 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y); 
    [self.view.layer renderInContext:ctx]; 

    // Retrieve a UIImage from the current image context: 
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Return the image in a UIImageView: 
    return snapshotImage; 
} 
1

這裏是你做

+(UIImage *)maskImage:(UIImage *)image andMaskingImage:(UIImage *)maskingImage{ 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGImageRef maskImageRef = [maskingImage CGImage]; 
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskingImage.size.width, maskingImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); 
    if (mainViewContentContext==NULL) 
     return NULL; 
    CGFloat ratio = 0; 
    ratio = maskingImage.size.width/ image.size.width; 
    if(ratio * image.size.height < maskingImage.size.height) { 
     ratio = maskingImage.size.height/ image.size.height; 
    } 
    CGRect rect1 = {{0, 0}, {maskingImage.size.width, maskingImage.size.height}}; 
//// CHANGE THIS RECT ACCORDING TO YOUR NEEDS 

     CGRect rect2 = {{-((image.size.width*ratio)-maskingImage.size.width)/2 , -((image.size.height*ratio)-maskingImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}}; 
     CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); 
     CGContextDrawImage(mainViewContentContext, rect2, image.CGImage); 
     CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); 
     CGContextRelease(mainViewContentContext); 
     CGColorSpaceRelease(colorSpace); 
     UIImage *theImage = [UIImage imageWithCGImage:newImage]; 
     CGImageRelease(newImage); 
     return theImage; 
    } 

你需要有像這樣的

注意 掩蔽圖像不能有任何透明的方式。相反,透明區域必須是白色或黑白之間的某個值。黑色像素越是透明度越低。

enter image description here