簡體   English   中英

將UIImage分為兩半

[英]Cropping UIImage in two halfs

我正在嘗試一種將UIImage裁剪到中間的方法。 我需要一種方法可以在一張照片的左中間裁剪我,而另一種方法可以在另一張照片的右中間裁剪我。

到目前為止,我只有風景的左側和肖像的右側:

if(firstImage.imageOrientation == UIImageOrientationUp){   //LANDSCAPE
        UIImage *image = firstImage;
        CGImageRef tmpImgRef = image.CGImage;
        CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width /2.0, image.size.height));
        firstImage = [UIImage imageWithCGImage:topImgRef];
        CGImageRelease(topImgRef);
    }
    else{      //PORTRAIT
        UIImage *image = firstImage;
        CGImageRef tmpImgRef = image.CGImage;
        CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height/2.0));
        firstImage = [UIImage imageWithCGImage:topImgRef scale:image.scale orientation:UIImageOrientationRight];
        CGImageRelease(topImgRef);
    }

我不太喜歡Core Graphics,有人可以幫我嗎? 我現在想要的是一種告訴CGImageCreateWithImageInRect的方法,使我在景觀的右半部分而不是左半部分上修剪,在Portrait上我需要左半部分。

我沒有測試過,但我認為它可以工作:

//Portrait bottom half
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.width,image.height/2.0),NO,0);
[image drawAtPoint:CGPointMake(0, -image.size.height/2.0) blendMode:kCGBlendModeCopy alpha:1.0];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Landscape right half
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.width/2.0,image.height),NO,0);
[image drawAtPoint:CGPointMake(-image.size.width/2.0,0) blendMode:kCGBlendModeCopy alpha:1.0];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

暫無
暫無

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

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