Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 以编程方式从iphone屏幕的特定部分裁剪图像_Objective C_Iphone - Fatal编程技术网

Objective c 以编程方式从iphone屏幕的特定部分裁剪图像

Objective c 以编程方式从iphone屏幕的特定部分裁剪图像,objective-c,iphone,Objective C,Iphone,NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init]; CGSize contextSize=CGSizeMake(320400); UIGraphicsBeginImageContext(self.view.bounds.size) 从主屏幕中提取部分图像 在UIGraphicsBeginImageContext中,我只能使用大小,是否有任何方法可以使用CGRect或其他方法从屏幕的特定部分提取图像,例如(x,y,320,400)类似这样的东西

NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init]; CGSize contextSize=CGSizeMake(320400); UIGraphicsBeginImageContext(self.view.bounds.size)

从主屏幕中提取部分图像

在UIGraphicsBeginImageContext中,我只能使用大小,是否有任何方法可以使用CGRect或其他方法从屏幕的特定部分提取图像,例如(x,y,320,400)类似这样的东西希望这有帮助:

// Create new image context (retina safe)
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

// Create rect for image
CGRect rect = CGRectMake(x, y, size.width, size.height);

// Draw the image into the rect
[existingImage drawInRect:rect];

// Saving the image, ending image context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

这个问题实际上是其他几个问题的重复,包括:,但由于我花了一段时间才找到解决方案,我将再次交叉发布

在寻求更容易理解(并以Swift书写)的解决方案时,我得出以下结论:


我希望能够根据纵横比从一个区域进行裁剪,并根据外部边界范围进行缩放。以下是我的变体:

import AVFoundation
import ImageIO

class Image {

    class func crop(image:UIImage, crop source:CGRect, aspect:CGSize, outputExtent:CGSize) -> UIImage {

        let sourceRect = AVMakeRectWithAspectRatioInsideRect(aspect, source)
        let targetRect = AVMakeRectWithAspectRatioInsideRect(aspect, CGRect(origin: CGPointZero, size: outputExtent))

        let opaque = true, deviceScale:CGFloat = 0.0 // use scale of device's main screen
        UIGraphicsBeginImageContextWithOptions(targetRect.size, opaque, deviceScale)

        let scale = max(
            targetRect.size.width / sourceRect.size.width,
            targetRect.size.height / sourceRect.size.height)

        let drawRect = CGRect(origin: -sourceRect.origin * scale, size: image.size * scale)
        image.drawInRect(drawRect)

        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return scaledImage
    }
}
我发现有两件事让我感到困惑,那就是裁剪和调整大小。裁剪由传递给drawInRect的矩形原点处理,缩放由大小部分处理。在我的例子中,我需要将源上裁剪矩形的大小与相同纵横比的输出矩形相关联。然后输出/输入比例因子,这需要应用到drawRect(传递到drawInRect)

一个警告是,这种方法有效地假设您正在绘制的图像大于图像上下文。我还没有对此进行测试,但我认为您可以使用此代码来处理裁剪/缩放,但可以显式地将scale参数定义为前面提到的scale参数。默认情况下,UIKit根据屏幕分辨率应用乘数

最后,应该注意的是,这种UIKit方法比CoreGraphics/Quartz和Core Image方法的级别更高,似乎可以处理图像方向问题。还值得一提的是,它的速度非常快,仅次于ImageIO,根据这里的帖子:

import AVFoundation
import ImageIO

class Image {

    class func crop(image:UIImage, crop source:CGRect, aspect:CGSize, outputExtent:CGSize) -> UIImage {

        let sourceRect = AVMakeRectWithAspectRatioInsideRect(aspect, source)
        let targetRect = AVMakeRectWithAspectRatioInsideRect(aspect, CGRect(origin: CGPointZero, size: outputExtent))

        let opaque = true, deviceScale:CGFloat = 0.0 // use scale of device's main screen
        UIGraphicsBeginImageContextWithOptions(targetRect.size, opaque, deviceScale)

        let scale = max(
            targetRect.size.width / sourceRect.size.width,
            targetRect.size.height / sourceRect.size.height)

        let drawRect = CGRect(origin: -sourceRect.origin * scale, size: image.size * scale)
        image.drawInRect(drawRect)

        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return scaledImage
    }
}