Iphone 裁剪图像截图
我目前正在替换代码中的UIGetScreenImage(),因为它会导致C99错误 我将它替换为,Iphone 裁剪图像截图,iphone,objective-c,ios,ipad,uiimage,Iphone,Objective C,Ios,Ipad,Uiimage,我目前正在替换代码中的UIGetScreenImage(),因为它会导致C99错误 我将它替换为,screenshot方法返回一个UIImage 我尝试裁剪和保存图像如下: UIImage *returnedImage = [self screenshot]; // Get image to save CGImageRef screen = returnedImage.CGImage; UIImage *img = [UIImage imageWithCGImage:screen]; // I
screenshot
方法返回一个UIImage
我尝试裁剪和保存图像如下:
UIImage *returnedImage = [self screenshot];
// Get image to save
CGImageRef screen = returnedImage.CGImage;
UIImage *img = [UIImage imageWithCGImage:screen];
// Image cropping
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 5, 768, 1004));
UIImage *img2Save = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(img2Save, self, nil, nil);
在返回它之前,我还尝试过在屏幕截图方法中裁剪它,但没有效果
两次尝试都会保存从屏幕截图
方法返回的未截取的UIImage
有什么明显的东西我漏掉了吗?我已查看,但看不到连接。请尝试以下代码
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//create a context to do our clipping in
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//create a rect with the size we want to crop the image to
//the X and Y here are zero so we start at the beginning of our
//newly created context
CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
CGContextClipToRect( currentContext, clippedRect);
//create a rect equivalent to the full size of the image
//offset the rect by the X and Y we want to start the crop
//from in order to cut off anything before them
CGRect drawRect = CGRectMake(rect.origin.x * -1,
rect.origin.y * -1,
imageToCrop.size.width,
imageToCrop.size.height);
//draw the image to our clipped context using our offset rect
CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
//pull the image from our cropped context
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
//pop the context to get back to the default
UIGraphicsEndImageContext();
//Note: this is autoreleased
return cropped;
}
然后用如下方法调用代码:
- (void)drawRect:(CGRect)rect
{
//draw the whole lights off image
//the on images will be drawn overtop
[lightsOffImage drawInRect:rect];
//if we don't have any lights on... no point in continuing
if( numberOfLightsOn < 1 )
return;
//figure out the dimensions of numberOfLights on bulbs
CGSize croppedSize = CGSizeMake(LIGHT_WIDTH * numberOfLightsOn, LIGHT_HEIGHT);
CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);
//get the "on" bulbs by cropping the image
UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];
//create a rect to draw the newly cropped on images to
CGRect lightsOnRect = CGRectMake(LIGHT_BULB_OFFSET_X,
LIGHT_BULB_OFFSET_Y,
croppedSize.width,
croppedSize.height);
//draw the "on" lights
[cropped drawInRect:lightsOnRect];
//cropped is autoreleased so no need to worry about cleanup
}
-(void)drawRect:(CGRect)rect
{
//画出整个灯光关闭图像
//图像上的图像将绘制在顶部
[lightsOffImage drawInRect:rect];
//如果我们没有任何灯亮着…继续下去就没有意义了
if(numberOfLightsOn<1)
返回;
//算出灯泡上的灯数的尺寸
CGSize CROPTEDSIZE=CGSizeMake(灯光宽度*灯光数量、灯光高度);
CGRect CLIPEDRECT=CGRectMake(0,0,croppedSize.width,croppedSize.height);
//通过裁剪图像获得“开启”灯泡
UIImage*cropped=[自映像通过裁剪:lightsOnImage toRect:clippedRect];
//创建一个矩形以绘制新裁剪的图像
CGRect lightsOnRect=CGRectMake(灯泡偏移量),
灯泡偏移量,
croppedSize.width,
裁剪尺寸、高度);
//绘制“打开”灯
[裁剪图纸,包括:lightsOnRect];
//裁剪是自动删除的,因此无需担心清理
}
希望这段代码能对您有所帮助。Hi@neon samuel,我曾尝试从另一个so线程调用该函数。发生的情况是,它似乎颠倒了图片并缩小了图像。这是因为我传递给它的坐标吗?我认为它不会倒过来,因为我传递给该方法的前两个参数是0。