2015-04-27 51 views
1

我想使用相機膠捲的最後一張圖像作爲我的UIButton的背景圖像。當視圖加載時,按鈕會正確顯示最後一張照片。但是,當我拍攝一張圖片時,UIbutton中的背景圖像沒有更新。下面是我使用,以獲得最後一張照片Swift - 從滾動的最後一張照片更新UIButton圖像

func lastPhoto() { 
    let imgManager = PHImageManager.defaultManager() 
    var fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)] 
    if let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) { 
     imgManager.requestImageForAsset(fetchResult.lastObject as PHAsset, targetSize: self.view.frame.size, contentMode: PHImageContentMode.AspectFill, options: nil, resultHandler: { (image, _) in 
      self.rollBtn.setBackgroundImage(image, forState: .Normal) 
     }) 
    } 
} 

不知道這是否是相關的代碼,但這裏是代碼段的我用它來拍照

switch (camMgr.cameraOutputMode) { 
    case .StillImage: 

     camMgr.capturePictureWithCompletition({ (image, error) -> Void in 

      if let capturedImage = image? { 
       println("capture image") 

       //animate button effect 
       UIView.animateWithDuration(0.3, animations: { 
        sender.backgroundColor = UIColor.greenColor() 
        }, completion: {(finished:Bool) in 
         println("inside") 
         sender.backgroundColor = UIColor.whiteColor() 
         self.lastPhoto() 
       }) 
       UIView.animateWithDuration(0.1, animations: { 
        self.buttonView.backgroundColor = UIColor.blackColor() 
        }, completion: {(finished:Bool) in 
         println("inside") 
         self.buttonView.backgroundColor = UIColor.clearColor() 
         self.lastPhoto() 
       }) 

      } 
     }) 

    case .VideoWithMic, .VideoOnly: 
     sender.selected = !sender.selected 
     sender.backgroundColor = sender.selected ? UIColor.redColor() : UIColor.greenColor() 
     if sender.selected { 
      camMgr.startRecordingVideo() 
     } else { 
      camMgr.stopRecordingVideo({ (videoURL, error) -> Void in 
       println(videoURL) 
       if let errorOccured = error? { 
        UIAlertView(title: "Error occured", message: errorOccured.localizedDescription, delegate: nil, cancelButtonTitle: "OK").show() 
       } 
      }) 
     } 
    } 
} 

我還是新對這整個編碼的事情,請任何幫助將很好。非常感謝你。

+0

這裏調用了所有這些代碼,即'viewDidLoad'或者一些初始化函數? – Eendje

回答

1

試試這個,我不確定你需要取回最後一張照片。就像這樣更新UIButton背景圖片。應該給你以後的效果。讓我知道它是否修復它。

 if let capturedImage = image? { 
      println("capture image") 

      //animate button effect 
      UIView.animateWithDuration(0.3, animations: { 
       sender.backgroundColor = UIColor.greenColor() 
       }, completion: {(finished:Bool) in 
        println("inside") 
        sender.backgroundColor = UIColor.whiteColor() 
      }) 
      UIView.animateWithDuration(0.1, animations: { 
       self.buttonView.backgroundColor = UIColor.blackColor() 
       }, completion: {(finished:Bool) in 
        println("inside") 
       self.buttonView.backgroundColor = UIColor.clearColor() 
       self.rollBtn.setBackgroundImage(capturedImage, forState: .Normal) 
      }) 

     } 
    }) 
+0

它工作了!非常感謝你。 – RandomBytes

相關問題