码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——项目实战Swift篇&swift 2.0项目开发总结二(开发常用)

时间:2015-09-25 00:06:15      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

 swift 2.0项目开发总结二(开发常用)
 
 
一:相册中选择相片到App指定位置
随 着相机像素的提高,实际用户选择的图片都是很大的,有的高达5.6M,如果直接使用用户选着的图片,非常消耗内存,并且也用不到这么高像素的图片,可以当 用户选着好图片后,在UIImagePickerController对应的代理方法中,先将图片进行重新绘制为需要的大小,在设置给iconView
 
 1 /// MARK: 摄像机和相册的操作和代理方法
 2 extension MeViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
 3 
 4   /// 打开照相功能
 5   private func openCamera() {
 6       if UIImagePickerController.isSourceTypeAvailable(.Camera) {
 7           pickVC.sourceType = .Camera
 8           self.presentViewController(pickVC, animated: true, completion: nil)
 9       } else {
10           SVProgressHUD.showErrorWithStatus("模拟器没有摄像头,请链接真机调试", maskType: SVProgressHUDMaskType.Black)
11       }
12   }
13 
14   /// 打开相册
15   private func openUserPhotoLibrary() {
16       pickVC.sourceType = .PhotoLibrary
17       pickVC.allowsEditing = true
18       presentViewController(pickVC, animated: true, completion: nil)
19   }
20 
21   func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
22       // 对用户选着的图片进行质量压缩,上传服务器,本地持久化存储
23       if let typeStr = info[UIImagePickerControllerMediaType] as? String {
24           if typeStr == "public.image" {
25               if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
26                   var data: NSData?
27                   let smallImage = UIImage.imageClipToNewImage(image, newSize: iconView!.iconButton.size)
28                   if UIImagePNGRepresentation(smallImage) == nil {
29                       data = UIImageJPEGRepresentation(smallImage, 0.8)
30                   } else {
31                       data = UIImagePNGRepresentation(smallImage)
32                   }
33 
34                   if data != nil {
35                       do {
36                           // TODO: 将头像的data传入服务器
37                           // 本地也保留一份data数据
38                           try NSFileManager.defaultManager().createDirectoryAtPath(theme.cachesPath, withIntermediateDirectories: true, attributes: nil)
39                       } catch _ {
40                       }
41                       NSFileManager.defaultManager().createFileAtPath(SD_UserIconData_Path, contents: data, attributes: nil)
42 
43                       iconView!.iconButton.setImage(UIImage(data: NSData(contentsOfFile: SD_UserIconData_Path)!)!.imageClipOvalImage(), forState: .Normal)
44 
45                   } else {
46                       SVProgressHUD.showErrorWithStatus("照片保存失败", maskType: SVProgressHUDMaskType.Black)
47                   }
48               }
49           }
50       } else {
51           SVProgressHUD.showErrorWithStatus("图片无法获取", maskType: SVProgressHUDMaskType.Black)
52       }
53 
54       picker.dismissViewControllerAnimated(true, completion: nil)
55   }
56 
57   func imagePickerControllerDidCancel(picker: UIImagePickerController) {
58       pickVC.dismissViewControllerAnimated(true, completion: nil)
59   }
60 }
61  

 

 

 
二:摇一摇功能的实现
 
 1 override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
 2       tableView!.hidden = true
 3       let animateDuration: NSTimeInterval = 0.3
 4       let offsetY: CGFloat = 50
 5 
 6       UIView.animateWithDuration(animateDuration, animations: { () -> Void in
 7           self.yaoImageView1.transform = CGAffineTransformMakeTranslation(0, -offsetY)
 8           self.yaoImageView2.transform = CGAffineTransformMakeTranslation(0, offsetY)
 9 
10           }) { (finish) -> Void in
11               let popTime = dispatch_time(DISPATCH_TIME_NOW,Int64(0.5 * Double(NSEC_PER_SEC)))
12               dispatch_after(popTime, dispatch_get_main_queue(), { () -> Void in
13 
14                   UIView.animateWithDuration(animateDuration, animations: { () -> Void in
15                       self.yaoImageView1.transform = CGAffineTransformIdentity
16                       self.yaoImageView2.transform = CGAffineTransformIdentity
17                       }, completion: { (finish) -> Void in
18 
19                           self.loadShakeData()
20                           // 音效
21                           AudioServicesPlayAlertSound(self.soundID!)
22                   })
23               })
24       }

 

 

iOS开发——项目实战Swift篇&swift 2.0项目开发总结二(开发常用)

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4836859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!