2012-11-27 247 views
1

我怎麼能只顯示原始圖像的部分在一個UIImageView在一個UIImageView只顯示原始圖像的部分

這個問題可能很熟悉,老,但是對於再次詢問原因,我在這些答案的幫助下找不到一個可行的想法 許多人說設置image.contentMode = UIViewContentModeCenter; (但不工作)

我需要一個幾乎包含原始圖像中心的矩形,我如何得到這個?

當我向應用程序顯示靜態圖像並將UIImageVie的內容模式設置爲Aspect Fill時,我確實做到了這一點。 但是,這是不是在當我顯示來自URL的圖像,並使用NSData的

添加我的代碼和圖片

NSString *weburl = @"http://topnews.in/files/Sachin-Tendulkar_15.jpg"; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 108, 86)]; 
    NSURL *url = [NSURL URLWithString:weburl]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    imageView.image = [UIImage imageWithData:data]; 

    [self.view addSubview:imageView]; 

Original Image Current outPut need

回答

7

如果您添加的UIImageView形式廈門國際銀行,你可以在那裏找到一個「模式」屬性,然後你可以看到,並從那裏設置不同的模式(從界面生成器)。或者通過編程,您可以通過

imageView.contentMode = UIViewContentModeCenter; 
imageView.clipsToBounds = YES; 
+0

atleast請讀取問題..這不是關於設置內容模式 –

+0

現在嘗試。寫下你的評論 – rakeshNS

+0

現在你來了一聲巨響.....感謝哥們,非常感謝你 現在你值得+1 ....和接受 –

4

對於這種情況可行的輸出,您需要根據您的要求裁剪圖像。

裁剪代碼如下,可以使用。

-(UIImage *) CropImageFromTop:(UIImage *)image 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 12, image.size.width, image.size.height - 12)); 
    UIImage *cropimage = [[[UIImage alloc] initWithCGImage:imageRef] autorelease]; 
    CGImageRelease(imageRef); 
    return cropimage; 
} 
0

您嘗試縮放圖像設定不同的模式,然後添加UIImageView的形象,並設置中心的形象則是在中心。灰度圖像的代碼是

-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

}

那麼你設置圖像的中心,並添加圖像視圖我希望這是工作

4

試試這個:

self.imageView.layer.contentsRect = CGRectMake(0.25, 0.25, 0.5, 0.5); 

self.imageView將顯示圖像的中間部分。你可以計算自己所需的CGRect值

+0

非常感謝你+1投票 – ashokdy

+0

我碰到這個並想知道,我可以顯示圖像的一部分,然後當我調整圖像視圖,顯示整個圖像? – Septronic