2016-07-02 16 views
0

UIImageを動的にロードします。それらは異なるサイズである。私が試みたことは、画像をダウンロードした後、私のUIImageViewフレームをリセットしました。動的高さをUIImageViewsをプログラムで素早く作成する

let imgVw:UIImageView! = UIImageView.init(frame: CGRectMake(5, self.otherVidY, imgvwWidth, scrollH-10)) 
     imgVw.image=UIImage(named: "NotFound") 
     self.scroll.addSubview(imgVw) 

     let vwY = (self.otherVidY+scrollH-10)-30 
     let view: UIView = UIView.init(frame: CGRectMake(5, vwY, imgvwWidth, 30)) 
     view.backgroundColor=UIColor.blackColor() 
     self.scroll.addSubview(view) 

     if let imgURL = NSURL(string: urlString) 
     { 
      do { 
       if let img = imageCache[urlString] { 
        imgVw.image = img 
       } 

       else 
       { 
        // The image isn't cached, download the img data 
        // We should perform this in a background thread 
        let request: NSURLRequest = NSURLRequest(URL: imgURL) 
        let mainQueue = NSOperationQueue.mainQueue() 
        NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 
         if error == nil { 
          // Convert the downloaded data in to a UIImage object 
          let image = UIImage(data: data!) 
          let resizedImage = self.resizeImage(image!, newWidth: imgVw.frame.size.width, imageView: imgVw) 
          // Store the image in to our cache 
          self.imageCache[urlString] = resizedImage 
          // Update the cell 
          dispatch_async(dispatch_get_main_queue(), { 
           imgVw.image = resizedImage 
           var rect = imgVw.frame 
           rect.size.height = resizedImage.size.height 
           imgVw.frame = rect 
           self.otherVidY = imgVw.frame.origin.y+imgVw.frame.size.height+5 

          }) 
         } 
         else { 
          print("Error: \(error!.localizedDescription)") 
         } 
        }) 
       } 

      } 

     } 

     self .otherVidY=imgVw.frame.size.height+imgVw.frame.origin.y+5 
     self.scroll.contentSize=CGSizeMake(self.screenWidth, self.otherVidY+imgVw.frame.size.height) 



    } 

しかし、私は次の画像のY値を設定するときに問題に直面しています。その重なり合っている。この問題を解決する方法。私を助けてください。 ありがとう

答えて

0

ここで、あなたは起源に対処しなければならず、それはあなたのロジックにのみ依存します。あなたは使うことができます。

object(View).frame.size.origin 

それはあなたのビューのX、Y及び

view.frame.size.height & view.frame.size.width

それはあなたの身長と体重を与えるを与えます。

次に、ImageViewに必要なCGRectを設定する必要があります。

関連する問題