2016-06-13 4 views
1

이미지 크기를 프로그래밍 방식으로 화면 크기에 맞게 조정하려고했지만 내 응용 프로그램을 빌드 할 때 이미지가 표시되지 않습니다. 나는 빈 화면을 보았고, 내가 잘못한 것을 누군가 알 수 있습니까?크기 조정 UIImage 프로그래밍 방식으로 작동하지 않습니다.

class ViewControllerSport: UIViewController { 

@IBOutlet weak var FotoSport: UIImageView! 

let screen = UIScreen.mainScreen().bounds 



override func viewDidLoad() { 
    super.viewDidLoad() 

    FotoSport.frame = CGRect(x: 20, y: 20, width: screen.width * 0.5, height: screen.width * 0.5) 
    FotoSport.image = UIImage(named: "Blokker") 


} 
+0

사용하는 이름 지정 규칙에 그냥 빨리 메모, 클래스는 lowerCaseLetters를 사용해야하는 변수 동안 UpperCaseLetters로 이름을 지정해야합니다. –

+0

이미지보기와 관련하여 레이아웃 제약 조건을 사용하고 있습니까? –

+0

당신은 대문자와 소문자에 대해 완전히 옳았습니다.이 이미지에서 실수 였을 것입니다. 이 View Controller에서는 Image View를 추가하고이 코드를 작성하는 것을 제외하고는 지금까지 아무 것도하지 않았습니다. –

답변

1

다음 함수는 이미지의 크기를 조정. 이미지와 원하는 크기의 두 가지 인수가 필요합니다.

func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage { 
    let size = image.size 

    let widthRatio = targetSize.width/image.size.width 
    let heightRatio = targetSize.height/image.size.height 

    // Figure out what our orientation is, and use that to form the rectangle 
    var newSize: CGSize 
    if(widthRatio > heightRatio) { 
     newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio) 
    } else { 
     newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio) 
    } 

    // This is the rect that we've calculated out and this is what is actually used below 
    let rect = CGRectMake(0, 0, newSize.width, newSize.height) 

    // Actually do the resizing to the rect using the ImageContext stuff 
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) 
    image.drawInRect(rect) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 

사용법 :

self.ResizeImage(UIImage(named: "MyImage.png")!, targetSize: CGSizeMake(320.0, 700.0)) 

참조 링크 : Resize image

스위프트 3.0 :

func ResizeImage(_ image: UIImage, targetSize: CGSize) -> UIImage? { 
    let size = image.size 

    let widthRatio = targetSize.width/image.size.width 
    let heightRatio = targetSize.height/image.size.height 

    // Figure out what our orientation is, and use that to form the rectangle 
    var newSize: CGSize 
    if(widthRatio > heightRatio) { 
     newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio) 
    } else { 
     newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio) 
    } 

    // This is the rect that we've calculated out and this is what is actually used below 
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height) 

    // Actually do the resizing to the rect using the ImageContext stuff 
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) 
    image.draw(in: rect) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 

사용법 :

self.ResizeImage(UIImage(named: "MyImage.png")!, targetSize: CGSize(width: 320.0, height: 700.0)) 
+0

고마워요! 이것은 매력처럼 작동했습니다! –

+0

당신은 친구를 환영합니다. –

1

첫째, UIImage(named: "Blokker") 이미지 또는 전무를 반환되어 있는지 확인합니다 : 이것은 내 코드입니다

(크기 조정 이미지에 대한 몇 가지 다른 스레드에서 배웠습니다). 이 이미지를 반환 있다면 ,있는 UIImageView에서 이미지를 확장하기 위해 당신은이 중 하나를 사용할 수 있습니다

UIViewContentModeScaleToFill; 
UIViewContentModeScaleAspectFit; 
UIViewContentModeScaleAspectFill; 

ScaleToFill은 이미지를 확장 할 수 있습니다.

AspectFit 및 AspectFill은 이미지의 가로/세로 비율을 조절합니다.

맞추기 이미지가 모두 표시 될 때까지 이미지의 크기를 조정합니다. 채우기는 해당면 중 하나가 UIImageView면 중 하나와 같아 질 때까지 이미지의 크기를 조절합니다.

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/Art/scale_aspect.jpg

관련 문제