2016-09-03 5 views
0

私が現在持っているコードはfirebaseからプロファイル画像を取得し、マップ上にマーカーアイコンとして表示します。マーカーアイコンをUIImageに設定し、バックグラウンドで適切にサイズを変更してアイコンとして表示させる

   self.ref.child("users").child(location.key as! String).child("userPhoto").observeSingleEventOfType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in 
        if(snapshot.exists()) { 

         let profileUrl = snapshot.value as? String 
         let url = NSURL(string: profileUrl!) 
         NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in 

          if error != nil { 
           print(error) 
           return 
          } 

          dispatch_async(dispatch_get_main_queue(), { 

           if let downloadedImage = UIImage(data: data!) { 
            marker.icon = downloadedImage 

           } 


          }) 

         }).resume() 
        } 
       }) 

残念ながら、このコードは、残念ながら、私が望んでいないマップ上に本当に大きな画像を表示します。私はピン/マーカーのように見えるボーダー/バックグラウンドを持つ円形の画像が欲しい。このように:

Ideal Deign of The Marker Icon

誰もがこれを行う方法を知っていますか?

ご協力いただければ幸いです。 ありがとう!

P.S:私はあなたが提供するソリューションで何かコードを表示できるかどうかを知りたいと思っています。ありがとう!

+0

回答を迅速に保存してください。ありがとう! –

答えて

1
func changeImageDimensions(image: UIImage, newWidth: CGFloat, newHeight: CGFloat) -> UIImage { 
let widthRat = newWidth/image!.size.width 
let heightRat = newHeight/image!.size.height 
var newSize: CGSize = CGSize() 
     if(widthRat > heightRat) { 
      newSize = CGSizeMake(image!.size.width * heightRat, image!.size.height * heightRat) 
     } else { 
      newSize = CGSizeMake(image!.size.width * widthRat, image!.size.height * widthRat) 
    } 

    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)) 
    let frameRect = CGRectMake(0, 0, newSize.width, newSize.height) 
    image.drawInRect(frameRect) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 

self.ref.child("users").child(location.key as! String).child("userPhoto").observeSingleEventOfType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in 
       if(snapshot.exists()) { 

        let profileUrl = snapshot.value as? String 
        let url = NSURL(string: profileUrl!) 
        NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in 

         if error != nil { 
          print(error) 
          return 
         } 

         dispatch_async(dispatch_get_main_queue(), { 

          if let downloadedImage = UIImage(data: data!) { 
           marker.icon = changeImageDimensions (downloadedImage!,newWidth:CGFloat(150),newHeight:CGFloat(150)) 

          } 


         }) 

        }).resume() 
       } 
      }) 
関連する問題