【问题标题】:Crop and save visible region of UIImageView using AspectFill使用 AspectFill 裁剪和保存 UIImageView 的可见区域
【发布时间】:2013-09-07 09:54:19
【问题描述】:

我有一个全屏 UIImageView,其中的图像设置为 AspectFill(全出血),如何在使用 Aspect Fill 内容模式时仅保存图像的可见部分?

CGRect visibleRect;   
visibleRect.size= mImageView.frame.size;
CGImageRef cropped_img = CGImageCreateWithImageInRect(mImageView.image.CGImage, visibleRect);
UIImage *finalImage = [[UIImage alloc]initWithCGImage:cropped_img];
UIImageWriteToSavedPhotosAlbum(finalImage, nil, nil, nil);

这是我当前的代码,裁剪和保存工作,只是没有正确裁剪。有什么想法吗?

【问题讨论】:

    标签: ios objective-c uiimageview crop


    【解决方案1】:

    试试这个:

    UIGraphicsBeginImageContext(mImageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGImageRef image = CGBitmapContextCreateImage(context);
    width = CGImageGetWidth(image);
    height = CGImageGetHeight(image);
    CGImageRef cropped_img = CGImageCreateWithImageInRect(image, CGRectMake(0, 0, width, height));
    

    // 保存...

    CGImageRelease(image);
    UIGraphicsEndImageContext();
    

    【讨论】:

    • 我将 UIImageView 的属性设置为 setClipsToBounds:YES 和 contentMode = UIViewContentModeScaleAspectFill。它不适用于我的图像视图。
    猜你喜欢
    • 2011-11-10
    • 2020-01-19
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 2013-08-11
    • 2014-05-01
    相关资源
    最近更新 更多