簡體   English   中英

如何在iphone中裁剪特定圖像

[英]How to crop a particular image in iphone

我正在使用相機應用程序用戶正在拍照,但我想裁剪該圖像中的任何位置並將其發送到服務器。 我怎樣才能做到這一點?

查看此鏈接了解詳情:

http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

基本代碼:

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
   //create a context to do our clipping in
   UIGraphicsBeginImageContext(rect.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);

   //create a rect equivalent to the full size of the image
   //offset the rect by the X and Y we want to start the crop
   //from in order to cut off anything before them
   CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                rect.origin.y * -1,
                                imageToCrop.size.width,
                                imageToCrop.size.height);

   //draw the image to our clipped context using our offset rect
   CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);

   //pull the image from our cropped context
   UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

   //pop the context to get back to the default
   UIGraphicsEndImageContext();

   //Note: this is autoreleased
   return cropped;
}

我認為我可以為大量代碼提供更好的解決方案。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // do something......
    UIImage *croppedImage = [self imageByCropping:[UIImage imageNamed:@"SomeImage.png"] toRect:CGRectMake(10, 10, 100, 100)];
}
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
   CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
   UIImage *cropped = [UIImage imageWithCGImage:imageRef];
   return cropped;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM