CoreText实现图文混排和AlassetLibrary

首先贴上一个网站http://www.jianshu.com/p/6db3289fb05d 但是还是有点看不懂,

CoreText实现图文混排其实就是在富文本中插入一个空白的图片占位符的富文本字符串,通过代理设置相关的图片尺寸信息,根据从富文本得到的frame计算图片绘制的frame再绘制图片这么一个过程。

AlassetLibrary是一个资源库,可以从中获得图片视频等 这样可以看这张图


ALAssetsLibrary代表系统中的整个资源库,可以读取所有的相册数据,即ALAssetsGroup列表。使用ALAssetsLibrary可以访问资源库中的照片和视频,也可以保存照片和视频。

ALAssetsGroup代表资源库中的每一个资源集合。即每一个ALAssetsGroup就是用户在“照片”应用程序中所看到的相册集合。

随后再获取到图片ALAssets

ALAssetRepresentation代表资每一个资源文件的详细信息。获取ALAssetRepresentation的一种方式如下:

更详细的介绍
http://www.cokco.cn/thread-4613-1-1.html
下面直接上代码
现在是main界面的
import UIKit
import AssetsLibrary
class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
//资源库管理类
    @IBOutlet weak var collectionView:UICollectionView!
    var assetsLibrary = ALAssetsLibrary()
    //保存照片的集合
    var assets = [ALAsset]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
      var countOne = 0
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        //ALAssetsGroupAavedPhotos表示只读取相机胶卷(ALAssetsGroupAll则读取全部的相册)
        assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: { (group, stop) in
            print("is goin")
            if(group != nil){
                //通过枚举来获得所有的相册的照片
                let assetBlock : ALAssetsGroupEnumerationResultsBlock = {
                    (result: ALAsset!, index: Int, stop) in
                    if result != nil
                    {
                        self.assets.append(result)
                        countOne += 1
                    }
                }
                //依次执行这个block 相当于一个循环 个人认为
            group.enumerateAssetsUsingBlock(assetBlock)
            print("assets:\(countOne)")
            self.collectionView.reloadData()
            }
            
            }) { (fail) in
                print(fail)
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // CollectionView行数
 func collectionView(collectionView: UICollectionView,
                                 numberOfItemsInSection section: Int) -> Int {
        return assets.count;
    }
    //获取单元格
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // storyboard里设计的单元格
        let identify:String = "imageCell"
        // 获取设计的单元格,不需要再动态添加界面元素
        let cell = (self.collectionView?.dequeueReusableCellWithReuseIdentifier(
            identify, forIndexPath: indexPath))! as! imageCollectionViewCell
        let myAsset = assets[indexPath.item]
        let image = UIImage(CGImage: myAsset.thumbnail().takeUnretainedValue())
       cell.imageview.image = image
        return cell
    }
    
    //响应单元格的点击事件
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let myAssets = assets[indexPath.row]
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("image") as! ImageDetailViewController
        vc.myAsset = myAssets
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    //定义每个cell的边框大小
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    }
    //定义每个cell的大小
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 100,height: 100)
    }
 


}
随后点击每一个item会Push到一个新界面 来显示图片的信息
import UIKit
import AssetsLibrary
class ImageDetailViewController: UIViewController {
    var myAsset:ALAsset!
    //显示图片的信息
    @IBOutlet weak var textView:UITextView!
    //用于显示原图
    @IBOutlet weak var imageView:UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
//获取文件名
        let representation = myAsset.defaultRepresentation()
        self.title = representation.filename()
        //获取图片的信息
        textView.text = "日期:" + "\(myAsset.valueForProperty(ALAssetPropertyDate))"
            + "类型:\(myAsset.valueForProperty(ALAssetPropertyType))\n"
            + "位置:\(myAsset.valueForProperty(ALAssetPropertyLocation))\n"
            + "时长:\(myAsset.valueForProperty(ALAssetPropertyDuration))\n"
            + "方向:\(myAsset.valueForProperty(ALAssetPropertyOrientation))"
        //获取原图
        let imageBuffer = UnsafeMutablePointer<UInt8>.alloc(Int(representation.size()))
        let bufferSize = representation.getBytes(imageBuffer, fromOffset: Int64(0), length: Int(representation.size()), error: nil)
        let data = NSData(bytesNoCopy: imageBuffer, length: bufferSize, freeWhenDone: true)
        imageView.image = UIImage(data: data)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}
详情可见这个博客http://www.hangge.com/blog/cache/detail_763.html
至于photoKit 由于只能在ios9中用到 因此这里暂不涉及 自己还没理解 先贴上大神的博客
http://kayosite.com/ios-development-and-detail-of-photo-framework.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值