2014-03-24 87 views
0

我正在開發應用程序,在這個應用程序中我需要給出一個裁剪選項。一旦我從照相機或圖庫中選擇圖像,它應該在編輯頁面上打開,我們有橢圓形圖像,並帶有縮放&移動選項。一旦我們點擊應用,拍攝的圖像應該裁剪成橢圓形。橢圓形的作物圖像

現在下面的屏幕是從鳥舍sdk。但它有方形種植需要在橢圓形的種植&。我試圖定製它,但無法做到這一點。

enter image description here

任何人都可以給我建議最容易的實現這一目標的最佳合適的方式。

謝謝。

+4

你知道如何掩蓋圖像嗎?你知道如何創建一個橢圓形嗎?你知道如何繪製圖像上下文嗎? –

+0

是的,我知道掩蔽,我也可以創建橢圓形狀以及繪製到圖像上下文中。但我想知道是否有替代和最適合的選項可用或不可用。 – Mayur

+0

您也可以使用剪切路徑,如[我的答案在此]中所述(http://stackoverflow.com/questions/22642136/uiimage-fill-transparent-part-with-another-image/22644165#22644165)。 – user1118321

回答

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

這種類型的屏蔽可以執行,否則你用椰子控制

的.h文件: -

@interface yourClass : UIImageView 

@end 

.m文件: -

#import <QuartzCore/QuartzCore.h> 

    @implementation yourClass 

    - (void)awakeFromNib 
    { 
     [super awakeFromNib]; 
     CALayer *mask = [CALayer layer]; 
     mask.contents = (id)[[UIImage imageNamed:@"ovalMask.png"] CGImage]; 
     CGSize size = self.frame.size; 
     mask.frame = CGRectMake(0, 0, size.width, size.height); 
     self.layer.mask = mask; 
     [self.layer setMasksToBounds:YES]; 
    } 

@end 
+0

確切的工作解決方案,你可以請詳細說明它如何使用這個 – Mayur

+0

採取一個黑色的橢圓形圖像,並作出了自定義類 – Spynet

+0

它不工作,我想要的。 – Mayur