图像没有在IOS
问题描述:
压缩使用下面的代码图像没有在IOS
UIImage *image = [UIImage imageNamed:@"1.png"];
CGSize newSize = CGSizeMake(100.0f, 100.0f);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_backgroundImageView.image = newImage;
答
如果要压缩图像的图像不会被压缩林
UIImage *img = [UIImage imageNamed:@"yourimagename"];
NSData *postData = UIImageJPEGRepresentation(img, 1.0);
NSInteger imgSizeBytes = [postData length];
double imgSizeKBytes = ceil((double)imgSizeBytes/1024);
NSString *strBytes;
if(imgSizeKBytes > 1024) {
double imgSizeMBytes = (double)imgSizeKBytes/1024;
strBytes = [NSString stringWithFormat:@"%.1f MB", imgSizeMBytes];
}
else {
strBytes = [NSString stringWithFormat:@"%d KB", (int)imgSizeKBytes];
}
imageView.image = [UIImage imageWithData:postData scale:0.1];
你可以通过你的要求的图像尺寸压缩使用下面的代码
NSData *postData = UIImageJPEGRepresentation(img, (double)(100-75)/100);
上面我给出了75.如果你想给你的大小,给那里。
您需要缩小图像尺寸? – vaibhav
我需要将图像放置在图像视图中。图像视图在故事板中。不是通过编程创建的。 – ios
,我需要将图像放在图像视图中,但图像视图放置在故事板中。图像不会失去他们的创意,无论模拟器和imageview .... – ios