2013-04-23 48 views
0

我的應用程序可以有許多imagesbackground。這images我需要下載,所以我不想單獨下載iPhone 4iPhone 5。我使用方法self.view.backgroundcolor = [UIColor colorWithPatternImage:[UIImage imagenamed:@"myImage.jpg"]]。我想下載一個imageiPhone 5,然後切割頂部和底部的邊界,以便與iPhone 4一起使用。我怎樣才能以最好的方式編程。哦,也許有我的問題最好的解決方案?Iphone 4和iPhone 5的圖像

回答

1

您可以使用此功能裁剪圖像定義您想要的矩形,因爲剩餘的圖像將被刪除 爲此,您需要CoreGraphics.framework,所以不要忘記添加到您的項目中。

- (UIImage *)imageByCroppingtoRect:(CGRect)rect fromImage:(UIImage *)image 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect); 
    UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return cropped; 
} 
1

試試這個

- (UIImage *)cropImage:(UIImage *)oldImage { 
    CGSize imageSize = oldImage.size; 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageSize.width,imageSize.height - 100),NO,0.); // this height you want to change 
    [oldImage drawAtPoint:CGPointMake(0, -46) blendMode:kCGBlendModeCopy alpha:1.];// from top to change X is never change 
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return croppedImage; 
}