Crop and save visible region of UIImageView using AspectFill

◇◆丶佛笑我妖孽 提交于 2019-12-11 01:49:56

问题


I have a fullscreen UIImageView with a image in it set to AspectFill (full bleed), how can I save only the visible portion of the image when its using the Aspect Fill content mode?

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);

This is my current code, cropping and saving works, its just not cropping correctly. Any ideas?


回答1:


Try this:

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));

// save...

CGImageRelease(image);
UIGraphicsEndImageContext();


来源:https://stackoverflow.com/questions/18604559/crop-and-save-visible-region-of-uiimageview-using-aspectfill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!