2013-03-18 44 views

Trả lời

3

Nếu bạn muốn tận dụng lợi thế của các tính chất thay đổi kích thước của UIImage, bạn cần phải sử dụng một UIImageView. Như bạn đã nhận thấy, nó không hoạt động nếu bạn đặt nó làm màu nền. Chỉ cần tạo một UIImageView và đặt nó sau UIScrollView của bạn với cùng một khung nếu nó phải được cố định hoặc bên trong UIScrollView của bạn với cùng kích thước với contentSize nếu bạn muốn cuộn ...

1

Thêm hình ảnh trong UIImageView và trong trường hợp của bạn thiết

yourImageView.contentMode = UIViewContentModeScaleAspectFill; 

bạn có thể đặt nó vào bất kỳ những điều sau đây

UIViewContentModeScaleToFill, 
UIViewContentModeScaleAspectFit, 
UIViewContentModeScaleAspectFill, 
UIViewContentModeRedraw, 
UIViewContentModeCenter, 
UIViewContentModeTop, 
UIViewContentModeBottom, 
UIViewContentModeLeft, 
UIViewContentModeRight, 
UIViewContentModeTopLeft, 
UIViewContentModeTopRight, 
UIViewContentModeBottomLeft, 
UIViewContentModeBottomRight 

Ans thêm UIImageView này trong bạn scrolView.

Một cách khác là bạn có thể sử dụng phương pháp sau để nhận/tạo kích thước hình ảnh tùy thích.

Sử dụng phương pháp với cụ Hightrộng sau đây với ảnh

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height 
{ 
    CGSize newSize = CGSizeMake(width, height); 
    float widthRatio = newSize.width/image.size.width; 
    float heightRatio = newSize.height/image.size.height; 

    if(widthRatio > heightRatio) 
    { 
     newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio); 
    } 
    else 
    { 
     newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio); 
    } 


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

Phương pháp này trở NewImage, với kích thước cụ thể mà bạn chỉ định.

0

Hãy thử điều này một

UIImage* backgroundImage =[[UIImage imageNamed:@"back.jpg"] 
          resizableImageWithCapInsets:UIEdgeInsetsMake(100, 0, 0, 0) ]; 
UIGraphicsBeginImageContextWithOptions(self.scrollView.bounds.size, NO, 0.0); 
[backgroundImage drawInRect:self.scrollView.bounds]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

self.scrollView.backgroundColor = [UIColor colorWithPatternImage:newImage]; 
Các vấn đề liên quan