UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图
?/*
???? 設置圓角,通過layer中的cornerRadius和masksToBounds即可。
??? ?
???? 自適應圖片寬高比例。通過UIViewContentModeScaleAspectFit設置,注意這個UIImageView的frame就不是init中的數據了。
??? ?
???? 同樣的UIImage圖片放入不同frame中的UIImageView就可以實現比例縮放了。只是UIImageView的大小改變了,
??? ?
???? */
??? UIImage* image = [UIImage imageNamed:@"back2.jpg"];? ?
??? UIImageView* imageView1 = [[[UIImageView alloc] initWithImage:image] autorelease];? ?
??? imageView1.frame = CGRectMake(0, 0, 300, 200);? ?
??? imageView1.center = CGPointMake(150, 200);
??? //設置圓角
??? imageView1.layer.cornerRadius = 8; ?
??? imageView1.layer.masksToBounds = YES;
?? ?
??? //自適應圖片寬高比例
??? imageView1.contentMode = UIViewContentModeScaleAspectFit; ?
??? [self.view addSubview:imageView1]; ?
?? ?
?? ?
??? //拉伸圖片
??? CGFloat capWidth = image.size.width / 2; ?
??? CGFloat capHeight = image.size.height / 2; ?
??? UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight];
??? UIImageView* imageView3 = [[[UIImageView alloc] initWithImage:stretchableImage] autorelease];
??? imageView3.frame = CGRectMake(0, 0, 300, 200);? ?
??? imageView3.center = CGPointMake(150, 200); ?
??? [self.view addSubview:imageView3];
?? ?
??? //改變frame改變
??? UIImageView* imageView4 = [[[UIImageView alloc] initWithImage:image] autorelease];
??? imageView4.frame = CGRectMake(0, 0, 300/2, 200/2);? ?
??? imageView4.center = CGPointMake(150, 200); ?
??? [self.view addSubview:imageView4];
?
//縮微圖- (UIImage *)generatePhotoThumbnail:(UIImage *)image {
? ? // Create a thumbnail version of the image for the event object.
? ? CGSize size = image.size;
? ? CGSize croppedSize;
? ? CGFloat ratioX = 75.0; ??
? ? CGFloat ratioY = 60.0;
? ? CGFloat offsetX = 0.0;
? ? CGFloat offsetY = 0.0;
?? ?
? ? // check the size of the image, we want to make it
? ? // a square with sides the size of the smallest dimension
? ? if (size.width > size.height) {
? ? ? ? offsetX = (size.height - size.width) / 2;
? ? ? ? croppedSize = CGSizeMake(size.height, size.height);
? ? } else {
? ? ? ? offsetY = (size.width - size.height) / 2;
? ? ? ? croppedSize = CGSizeMake(size.width, size.width);
? ? }
?? ?
? ? // Crop the image before resize
? ? CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
? ? CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
? ? // Done cropping
? ? // Resize the image
? ? CGRect rect = CGRectMake(0.0, 0.0, ratioX, ratioY); // 設置圖片縮微圖的區域((0,0),寬:75 ?高:60)
? ? UIGraphicsBeginImageContext(rect.size);
? ? [[UIImage imageWithCGImage:imageRef] drawInRect:rect];
? ? UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
? ? UIGraphicsEndImageContext();
? ? // Done Resizing
? ? return thumbnail;
}
總結
以上是生活随笔為你收集整理的UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到拉屎是什么意思
- 下一篇: 你的工作单位也需善待