2014-11-09 2 views
0

Привет, это мой первый пост. Спасибо за помощь.Как центрировать мое изображениеПросмотреть в другом ImageView программно

Я создал UIImageView и добавил эффект размытия над ним. Теперь я добавил еще один UIImageView в качестве подсмотра размытого изображения. Я пытаюсь понять, как я могу сделать свой рост и ширину пропорционально программно, чтобы он выглядел хорошо на всех размерах экрана.

Я выяснил, как центрировать его, но не могу получить размер, чтобы хорошо выглядеть. Я хочу, чтобы высота была немного меньше, чем высота его надзора и его круг, чтобы ширина была равна высоте.

//create blurred profile picture 
    //first create the uiimageview 
    let blurProfilePic = UIImageView(frame: CGRectMake(0, 64, view.frame.size.width, view.frame.size.height/3.0)) 
    //assign it a value 
    blurProfilePic.image = UIImage(named: "placeholder") 
    //make sure it stays within the created bounds 
    blurProfilePic.layer.masksToBounds = true 
    //center the picture 
    blurProfilePic.contentMode = UIViewContentMode.Center 


    //create blur effect 
    var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView 
    visualEffectView.frame = blurProfilePic.bounds 

    //add blur layer to blurProfilePic 
    blurProfilePic.addSubview(visualEffectView) 

    //create the real profile pic 
    //arbitrary frame 
    let profilePic = UIImageView(frame: CGRectMake(50, 50, 180, 180)) 
    //the 180,180 needs to be changed so that they are sized proportionally for all screens 
    //center it inside the blurimage 
    profilePic.center = CGPointMake(blurProfilePic.frame.size.width/2, blurProfilePic.frame.size.height/2) 
    profilePic.image = UIImage(named: "placeholder") 
    profilePic.layer.borderWidth = 5 
    profilePic.layer.borderColor = UIColor.whiteColor().CGColor 
    profilePic.layer.cornerRadius = profilePic.frame.size.width/2 
    profilePic.clipsToBounds = true 


    blurProfilePic.addSubview(profilePic) 

    view.addSubview(blurProfilePic) 

ответ

0

Вам это поможет?

class ViewController: UIViewController { 
     let blurProfilePic = UIImageView() 
     let profilePic = UIImageView() 

     override func viewWillLayoutSubviews() { 
       blurProfilePic.frame = CGRect(x: 0, y: UIApplication.sharedApplication().statusBarFrame.height, width: view.bounds.width, height: view.bounds.height/3) 

       let blurProfilePicBounds = blurProfilePic.bounds 
       profilePic.frame.size = CGSize(width: blurProfilePicBounds.height, height: blurProfilePicBounds.height) 
       profilePic.center = CGPointMake(blurProfilePicBounds.width/2, blurProfilePicBounds.height/2) 
       profilePic.layer.cornerRadius = profilePic.frame.size.width/2 
     } 

     override func viewDidLoad() { 
       super.viewDidLoad() 

       //assign it a value 
       blurProfilePic.image = UIImage(named: "placeholder") 
       //make sure it stays within the created bounds 
       blurProfilePic.layer.masksToBounds = true 
       //center the picture 
       blurProfilePic.contentMode = .ScaleAspectFill 

       //create blur effect 
       var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView 
       visualEffectView.frame = blurProfilePic.bounds 
       visualEffectView.autoresizingMask = .FlexibleWidth | .FlexibleHeight 
       //add blur layer to blurProfilePic 
       blurProfilePic.addSubview(visualEffectView) 

       profilePic.image = UIImage(named: "placeholder") 
       profilePic.contentMode = .ScaleAspectFill 
       profilePic.layer.borderWidth = 5 
       profilePic.layer.borderColor = UIColor.whiteColor().CGColor 
       profilePic.clipsToBounds = true 

       blurProfilePic.addSubview(profilePic) 

       view.addSubview(blurProfilePic) 
     } 
} 

Update:

Это снимок, когда я установил profilePic.contentMode и blurProfilePic.conteMode как к .ScaleAspectFill, кажется, хорошо

enter image description here

Если установить profilePic.contentMode в .ScaleAspectFit, вы можете см. изображение не изменяется, чтобы заполнить круг.

enter image description here

+0

К сожалению, это не решит проблему. Если я запустил это в iphone 4/4s, изображение отключится. Я хочу, чтобы изображение было правильно настроено для всех iphones ... его уже сосредоточено для всех iphone, но я хочу, чтобы изображение стало больше/уменьшалось с использованием свойств фрейма, но я не могу понять, как это сделать. –

+0

@JeremySh Извините, я обновил его снова, проверьте его. –

+0

спасибо, что исправляет проблему изменения кругового изображения в рамки blurredPic ... это может быть вопросом для другой темы, но теперь фактическое изображение не изменяет размер, чтобы заполнить круг ... Я попытался переключиться .ScaleAspectFit в .ScaleAspectFill и некоторые другие, но не повезло ... любые предложения? Спасибо за вашу помощь –

1

Вместо совпадающих центров вы пытались использовать функцию CGRectInset()? Таким образом, вы можете назначить кадр родительского UIImageView дочернему, вставляемому x, y точками, и вам не нужно будет беспокоиться о его центрировании.

Смежные вопросы