我正试图在手机上直接拍照后将图像文件上传到Parse.但它引发了一个例外:
由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'PFFile不能大于10485760字节'
这是我的代码:
在第一个视图控制器:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "getImage")
{
var svc = segue.destinationViewController as! ClothesDetail
svc.imagePassed = imageView.image
}
}
Run Code Online (Sandbox Code Playgroud)
在上传图像的视图控制器中:
let imageData = UIImagePNGRepresentation(imagePassed)
let imageFile = PFFile(name: "\(picName).png", data: imageData)
var userpic = PFObject(className:"UserPic")
userpic["picImage"] = imageFile`
Run Code Online (Sandbox Code Playgroud)
但我仍然需要将该照片上传到Parse.有没有办法减小图像的大小或分辨率?
我在用
UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate
Run Code Online (Sandbox Code Playgroud)
这些代表从我的画廊或我的相机中选择图像.那么,如何在选择图像后获得图像文件大小?
我想用这个:
let filePath = "your path here"
var fileSize : UInt64 = 0
do {
let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath)
if let _attr = attr {
fileSize = _attr.fileSize();
print(fileSize)
}
} catch {
}
Run Code Online (Sandbox Code Playgroud)
但是在这里我需要一条路径,但是如果没有路径,我只能通过图像文件获得?
我正在尝试将图形编程的图像缩放到照片库中.我正在尝试缩放图像以使其更小.我如何使用下面的代码使图像更小
image1.scale
Run Code Online (Sandbox Code Playgroud) 我在上传它们之前尽可能地减少图像的文件大小.现在我这样做:
if let firstImageData = UIImageJPEGRepresentation(pickedImage, 0.1) {
self.imgArray.append(firstImageData)
}
Run Code Online (Sandbox Code Playgroud)
这将拍摄来自相机或相册的任何图像,使其成为jpg并缩小其尺寸.
我已将设置设置为0.1但是当我上传图像时,大小仍然大约为300-350kb,有没有什么方法可以调整它们的大小,我的目标是50-70kb
我想缩小此网站 http://resizeimage.net的 图像大小,在使用下面的代码后,我的图像大小为1080 x 360,大小为120kb,但是当我使用该网站时,我的图像大小为58kb,或者有一个库或算法可以压缩JPEG文件
func resizeImage(_ image: UIImage, newHeight: CGFloat) -> UIImage {
let scale = newHeight / image.size.height
let newWidth = image.size.width * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
let imageData = UIImageJPEGRepresentation(newImage!, 0.5)! as Data
UIGraphicsEndImageContext()
return UIImage(data:imageData)!
}
Run Code Online (Sandbox Code Playgroud)