我正在使用Swift和Parse.com 为iOS制作应用程序
我试图让用户从图像选择器中选择一张图片,然后将所选图像调整为200x200像素,然后再上传到我的后端.
Parse.com有一个名为"AnyPic"的Instagram复制应用程序的教程,该应用程序提供了用于调整图像大小的代码,但它是在Objective-C ....
// Resize the image to be square (what is shown in the preview)
UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit
bounds:CGSizeMake(560.0f, 560.0f)
interpolationQuality:kCGInterpolationHigh];
// Create a thumbnail and add a corner radius for use in table views
UIImage *thumbnailImage = [anImage thumbnailImage:86.0f
transparentBorder:0.0f
cornerRadius:10.0f
interpolationQuality:kCGInterpolationDefault];
Run Code Online (Sandbox Code Playgroud)
如何在Swift中创建所选图片的200x200px版本(然后上传)?
而且,thumbnailImage函数在做什么?
在Swift编程中,如何裁剪图像并将其放在中心之后?
这就是我到目前为止所做的...我已成功裁剪图像,但我想把它放在中心之后
ImgView.image = OrigImage
var masklayer = CAShapeLayer()
masklayer.frame = ImgView.frame
masklayer.path = path.CGPath
masklayer.fillColor = UIColor.whiteColor().CGColor
masklayer.backgroundColor = UIColor.clearColor().CGColor
ImgView.layer.mask = masklayer
UIGraphicsBeginImageContext(ImgView.bounds.size);
ImgView.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
ImgView.image = image
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
更新:
let rect: CGRect = CGRectMake(path.bounds.minX, path.bounds.minY, path.bounds.width, path.bounds.height)
// Create bitmap image from context using the rect
let imageRef: CGImageRef = CGImageCreateWithImageInRect(image.CGImage, rect)
ImgView.bounds = rect
ImgView.image = UIImage(CGImage: imageRef)
Run Code Online (Sandbox Code Playgroud)
我能够通过获取path.bound和size来中心它并更改我的ImageView的边界.:)
我正在快速工作,我的要求是创建矩形区域
相机。我必须只捕获矩形内的部分
其余部分应显示为模糊。
我尝试了很多链接,但大多数都在 Obj-Ci 中,我知道我必须在 AVCapture 层上添加 UI 层。这个[单击此处]链接对我有帮助,但我无法实现我的目标。
我尝试减小故事板中 ImageView 的大小,但在这种情况下,我的相机在这个小图像视图中调整整个图像。
这是示例图像。
这是我现有的相机代码:
class VideoFeedMicr: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate
{
let outputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL)
let device: AVCaptureDevice? = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]
var camera: AVCaptureDevice? = nil
for device in devices {
if device.position == .Back {
camera = device
}
}
return camera
}()
var input: AVCaptureDeviceInput? = nil
var delegate: VideoFeedDelegateMicr? = nil
let session: AVCaptureSession = {
let session = AVCaptureSession() …Run Code Online (Sandbox Code Playgroud)