請參考下面的代碼。在這裏,我提供了一個imageview,並在兩個相同的圖像中分割相同的圖像。
- (void)splitImageView:(UIImageView*)imgView{
UIImage *image = imgView.image;
CGRect rectForDrawing = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGRect leftRect = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGRect rightRect = CGRectMake(imgView.frame.size.width/2, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGImageRef leftReference = CGImageCreateWithImageInRect([image CGImage], leftRect);
CGImageRef rightReference = CGImageCreateWithImageInRect([image CGImage], rightRect);
UIGraphicsBeginImageContextWithOptions(rectForDrawing.size, NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, rectForDrawing,flip(leftReference));
imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
imgViewLeft.frame = CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y,
imgView.frame.size.width/2, imgView.frame.size.height);
CGContextDrawImage(con, rectForDrawing,flip(rightReference));
imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
imgViewRight.frame = CGRectMake(imgView.frame.origin.x+imgView.frame.size.width/2, imgView.frame.origin.y,
imgView.frame.size.width/2, imgView.frame.size.height);
UIGraphicsEndImageContext();
[self.view addSubview:imgViewLeft];
[self.view addSubview:imgViewRight];
}
+1對您的問題有很好的解釋。 – kubilay 2012-09-04 17:28:56