简体   繁体   中英

Image for Iphone 4 and Iphone 5

My app can have many images for background . This images I need download, so I don't want download separately for iPhone 4 and iPhone 5 . I use method self.view.backgroundcolor = [UIColor colorWithPatternImage:[UIImage imagenamed:@"myImage.jpg"]] . I want download one image for iPhone 5 and then cut top and bottom borders for use with iPhone 4 . How can I do it programmatically with the best way. Oh maybe there are the best solutions for my problem?

You can crop image using this function define the rect you want as image remaining will be removed for this you need CoreGraphics.framework , so dont forget to add in your Project.

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

try this

- (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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM