【问题标题】:how to crop some portion of scaled, rotated image-view如何裁剪缩放的旋转图像视图的某些部分
【发布时间】:2011-08-27 02:56:27
【问题描述】:

我无法从图像视图中裁剪图像,该图像在裁剪前已缩放和旋转。使用现有的裁剪方法。但它仅适用于固定的图像视图。希望代码会有所帮助。

【问题讨论】:

    标签: iphone uiimageview crop image-rotation image-scaling


    【解决方案1】:

    终于成功裁剪旋转缩放的图片了。

    1) 需要裁剪整个缩放或旋转的图像。

    2) 从裁剪图像中裁剪矩形。您将获得最终缩放或旋转的裁剪图像。

    我发布示例代码,它可能对其他人有帮助。

    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    CGRect rect = CGRectMake(100,100 ,200, 200);//Rect we need from scaled or rotated image.
    CGImageRef cropped = CGImageCreateWithImageInRect(viewImage.CGImage, rect);
    UIImage *img =  [UIImage imageWithCGImage:cropped]; 
    CGImageRelease(cropped);
    

    谢谢。

    【讨论】:

    • 您的图像是旋转的还是视图?在我的例子中,imageview 被旋转和缩放,而不是图像本身。
    【解决方案2】:

    试试这个(如果你需要裁剪角落):

    UIImageView *imageSubview = [[UIImageView alloc] initWithFrame:CGRectMake(50.0f, 5.0f,80.0f, 60.0f)];
    
    [imageSubview setImage:actualImage]; 
    [imageSubview setContentMode:UIViewContentModeScaleToFill];
    [imageSubview.layer setCornerRadius:10.0];
    [imageSubview.layer setMasksToBounds:YES];
    [cell addSubview:imageSubview];
    

    只要增加半径,你就会把它变成圆形,或者沿着这条路走,你就会得到你的实际解决方案。:)

    【讨论】:

    • 感谢更新。我需要图像中间,或图像视图中的任何位置,如图像选择器图像编辑属性!
    【解决方案3】:
    UIImage *myImage = [UIImage imageNamed:@"photo.png"];
    
    CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0));
    CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
    
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
    [myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 
    
    CGImageRelease(croppedImage);
    

    它用于裁剪特定的高度和宽度

    【讨论】:

    • 感谢更新。但已经使用逻辑,使用固定图像。一旦缩放或旋转图像,它就不起作用,总是返回相同的图像。 CGRect rect = CGRectMake([tempView center].x-125,[tempView center].y-200 ,200, 200); CGImageRef 裁剪 = CGImageCreateWithImageInRect(imageView.image.CGImage, rect); UIImage *img = [UIImage imageWithCGImage:cropped]; //[UIImage imageWithCGImage:cropped scale:imageView.image.scaleorientation:imageView.image.imageOrientation]; CGImageRelease(裁剪);
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多