2019-08-19 16:08:44 dongdong_yan 阅读数 196

     当我们项目中有关于图片的功能时,就免不了需要用到图片上传。

     但是,我们要认识到,你要上传的图片大小,是否是必须的。当我们确定要上传的图片,只是用在手机端显示,并不是必须使用原图,这时,我们要在上传前,进行图片的压缩。

     swift提供了较简单的压缩方法:

     

var imageData: Data = image.jpegData(compressionQuality: 0.3) ?? Data()

      但是,这个方法有个问题,当压缩比例太小时,就会固定在某个极限值,我个人粗略估算,大约是0.02

       因此,当图片足够大,而我们只是用来当类似头像来使用时,上述方法就那以满足了。在此,我提供一个另外的方法:将图片重新绘制,以大量减小图片大小   

    /**
     *  等比率缩放
     */
    func scaleImage(scaleSize:CGFloat)->UIImage {
        let reSize = CGSize(width:self.size.width * scaleSize, height:self.size.height * scaleSize)
        return reSizeImage(reSize: reSize)
    }
    /**
     *  重设图片大小
     */
    func reSizeImage(reSize:CGSize)->UIImage {
        UIGraphicsBeginImageContextWithOptions(reSize,false,UIScreen.main.scale);
        self.draw(in: CGRect(x:0, y:0, width:reSize.width, height:reSize.height));
        let reSizeImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage();
        UIGraphicsEndImageContext();
        return reSizeImage;
    }

 

2019-10-25 16:18:07 kashjack 阅读数 21

pod 'GzipSwift'

let str = "欢迎来到CSDN"

 

//原始数据

let data = str.data(using: String.Encoding.utf8)!

print("压缩前的大小:\(data.count)字节")

 

//压缩后数据

let compressedData = try! data.gzipped()

print("压缩后的大小:\(compressedData.count)字节")

 

//解压缩

let originalData = try! compressedData.gunzipped()

let originalStr =  String(data: originalData, encoding: String.Encoding.utf8)

简单易用

2019-03-13 14:19:28 weixin_42012181 阅读数 209

后台往往要求上传压缩图片,大小不能超过多少。前端就需要对手机获取的图片做处理:

extension UIImage {
    func compressImageOnlength(maxLength: Int) -> Data? {
        let maxL = maxLength * 1024 * 1024
        var compress:CGFloat = 0.9
        let maxCompress:CGFloat = 0.1
        var imageData = UIImageJPEGRepresentation(self, compress)
        while (imageData?.count)! > maxL && compress > maxCompress {
            compress -= 0.1
            imageData = UIImageJPEGRepresentation(self, compress)
        }
        return imageData
    }
}

扩展UIImage类,调用compressImageOnlength方法,传参数maxLength指定压缩图片大小,返回图片的Data数据。

2018-11-28 11:00:12 jacob_ios 阅读数 256

swift 4.2

将图片压缩到制定的大小,使用二分法进行循环,让计算值接近目标值

func compressQuality(_ maxLength:NSInteger,_ cyles:Int = 6) -> Data {
        var compression:CGFloat = 1
        var data = self.jpegData(compressionQuality: compression)!
        if data.count < maxLength {
            return data
        }
        var max:CGFloat = 1
        var min:CGFloat = 0
        var bestData:Data = data
        for _ in 0..<cyles {
            compression = (max + min)/2
            data = self.jpegData(compressionQuality: compression)!
            if Double(data.count) < Double(maxLength)*0.9 {
                min = compression
                bestData = data
            } else if data.count > maxLength {
                max = compression
            } else {
                bestData = data
                break
            }
        }
        return bestData
    }

 

2017-04-01 16:14:32 qq_22879593 阅读数 3125

Swift3.0 指针

最近试着用Swift基于AudioQueue写一个音乐播放器,但是AudioQueue的API都是C函数,避免不了操作指针。经常卡在指针上,于是决定先静下心来学习一下Swift中的指针。baidu了很多,但是没有系统讲解Swift指针的文章,一边学一边记录,写下这些作为积累。这次学习分为四个部分:UnsafePointer, UnsafeMutablePointer, UnsafeRawPointer, UnsafeMutableRawPointer。有不对的地方欢迎指出!

在Swift中,指针由结构体 struct UnsafePointer<Pointee>struct UnsafeMutablePointer<Pointee> 表示,但是不能通过”&”获取一个UnsafePointerUnsafeMutablePointer的实例,只能作为inout参数使用。UnsafePointer相对于UnsafeMutablePointer,其所指向的地址的值是不可变的

一、UnsafeMutablePointer

UnsafeMutablePointe引用的内存有三种状态:

1. Not Allocated

2. Allocated but not initialized

3. Allocated and initialized

只有在状态3时,可以安全的使用pointee属性来setget

1.初始化

allocate() initialize() deinitialize deallocate() 这四个方法来管理这个指针,并确保指针的pointee不会出错

(1) allocate

let a_unsafe_mutable_pointer = UnsafeMutablePointer<Int>.allocate(capacity: 0)

Tips:

1. 需要指明要声明的指针类型

2. allocate是静态方法

(2) initialize

a_unsafe_mutable_pointer.initialize(to: 5)
//或
a_unsafe_mutable_pointer.pointee = 5

这两种方法的结果是相同的

(3) deinitialize

a_unsafe_mutable_pointer.deinitialize(count: 1)

该方法貌似没有真正deintialize,只是返回了一个指向相同地址的UnsafeMutableRawPointer,并且a_unsafe_mutable_pointer.pointee仍然是可以正常使用

(4) deallocate

a_unsafe_mutable_pointer.deallocate(capacity: 1)

此时a_unsafe_mutable_pointer不可用了。

2.互相转化

(1) UnsafeMutablePointer -> Swift

let a : Int = a_unsafe_mutable_pointer.pointee

(2) Swift -> UnsafeMutablePointer

let a : UnsafeMutablePointer = &a 

以上代码是错误的,只能作为函数参数使用,如下所示

func printPointer(ptr:UnsafeMutablePointer<Int>) {
        print("UnsafeMutablePointer:\(ptr)")
        print("pointee:\(ptr.pointee)")
}
printPointer(ptr:&a)

3.暂时绑定

将该内存暂时绑定为一种类型,例子中将Int型绑定为Int8

a_unsafe_mutable_pointer.initialize(to: 0x0102030A)
a_unsafe_mutable_pointer.withMemoryRebound(to: Int8.self, capacity: 1, {
    ptr in
    print(String(ptr.pointee,radix:16)) //1
    print(String(ptr.advanced(by: 1).pointee,radix:16)) //2
    print(String(ptr.advanced(by: 2).pointee,radix:16)) //3
    print(String(ptr.advanced(by: 3).pointee,radix:16)) //a
})

二、UnsafePointer

  1. UnsafePointer中的pointee属性只能get不能set
  2. UnsafePointer中没有allocate方法。

1.初始化

let a_unsafe_pointer = UnsafePointer<Int>.init(a_unsafe_mutable_pointer)

可以由UnsafeMutablePointerOpaquePointer或其他UnsafePointer创建一个UnsafePointer指针。其他与UnsafeMutablePointer类似

2.互相转换

(1) UnsafeMutablePointer -> UnsafePointer

let a_unsafe_pointer = UnsafePointer<Int>.init(a_unsafe_mutable_pointer)

(2) UnsafePointer -> UnsafeMutablePointer

let a_unsafe_mutable_pointer = UnsafePointer<Int>.init(mutating:a_unsafe_pointer)

(3) UnsafePointer -> Swift

let a : Int = a_unsafe_pointer.pointee

(4) Swift -> UnsafePointer

UnsafeMutablePointer类似,只能作为函数参数引用

3.withUnsafePointer方法

withUnsafePointer方法可以直接使用指针,但是不能改变pointee的值

var a = 0
var b = a
a = withUnsafePointer(to: &a, {
        ptr in
        return ptr.pointee + 2 
        //此时,新开辟空间,令a指向该地址,其值为2
        //b仍指向a原来的地址,值为0
})

var _ = withUnsafePointer(to: &b, {
            ptr in
            let size = MemoryLayout<Int>.size //8
            //advance(by:Int)移动指针到a
            let movedPointer = ptr.advanced(by: -size) 
            print("b:\(ptr.pointee)") //b = 0
            print("a:\(movedPointer.pointee)") //a = 2
        })

三、UnsafeMutableRawPointer

UnsafeMutableRawPointer按我的理解就是无类型的原始指针

1.分配内存

var size = MemoryLayout<Int>.size
//其中alignTo是开辟内存中的对齐,不是很了解,貌似会影响效率,太底层了,不太懂
var a_unsafe_mutable_raw_pointer = UnsafeMutableRawPointer.allocate(bytes: size, alignedTo: size)

这时,这个a_unsafe_mutable_raw_pointer感觉没啥用,还是需要转换为UnsafeMutablePointer来对内存进行操作。

2.初始化

初始化内存为Int类型,并赋值为1

a_unsafe_mutable_raw_pointer.initializeMemory(as: Int.self, to: 1)

3.绑定UnsafeMutablePointer

绑定了UnsafeMutablePointer,使用UnsafeMutablePointer才能对内存进行赋值,有两种方法绑定:

(1) bindMemory()

该方法绑定内存为指定类型并返回一个UnsafeMutablePointer<指定类型>的指针

var a_unsafe_mutable_pointer = a_unsafe_mutable_raw_pointer.bindMemory(to: Int.self, capacity: 1)

(2) assumingMemoryBound()

该方法意思是直接转换这个原始指针为一个UnsafeMutablePointer<指定类型>的指针

var a_unsafe_mutable_pointer = a_unsafe_mutable_raw_pointer.assumingMemoryBound(to: Int.self)

-
这样,就可以使用这个a_unsafe_mutable_pointer进行其他操作了。

4.deallocate

最后,需要释放内存,首先要deinitialize,再deallocate

a_unsafe_mutable_pointer.deinitialize()
a_unsafe_mutable_raw_pointer.deallocate(bytes: size, alignedTo: size)

5.转换

(1) Swift -> UnsafeMutableRawPointer

var a = 10
var ptr = UnsafeMutableRawPointer(&a)

(2) UnsafeMutableRawPointer -> Swift

其过程应当为UnsafeMutableRawPointer转换为UnsafeMutablePointer,再由UnsafeMutablePointer转换为Swift指针

四、UnsafeRawPointer

UnsafeRawPointer只能由其他指针用init方法得到,与UnsafePointer类似,没有allocate静态方法。但是,与UnsafeMutableRawPointer类似的有两种绑定方法,绑定成UnsafePointer指针。

iOS 大小端转换代码

阅读数 4508

ios 如何获取手机存储空间大小

博文 来自: L_weiguo
没有更多推荐了,返回首页