iOS画像のスケーリング
3308 ワード
需要背景
テストは私を探し当てて言います:“あなたのこの図はあまりにも丑くて、全体の协调性に深刻な影响を及ぼして、変えることができる小さい点”私は言います:“これは简単で、小さいcase”1分后、テストMMはまた来ました:“ピクチャーはどうしてすべて歪んでいますか?”「見てみろ」は本当に歪んでいて、明らかに鋸歯状です...不思議なことに私はそうした.- (UIImage *)imageScaled:(float)sizeScale
{
CGSize size = CGSizeMake(self.size.width * sizeScale, self.size.height * sizeScale);
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, self.size.width * sizeScale, self.size.height * sizeScale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
考えて、UIGraphicsBeginImageContextにポイントインしてみました// UIImage context
// The following methods will only return a 8-bit per channel context in the DeviceRGB color space.
// Any new bitmap drawing code is encouraged to use UIGraphicsImageRenderer in leiu of this API.
UIKIT_EXTERN void UIGraphicsBeginImageContext(CGSize size);
UIKIT_EXTERN void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN UIImage* __nullable UIGraphicsGetImageFromCurrentImageContext(void);
UIKIT_EXTERN void UIGraphicsEndImageContext(void);
UIKIT_EXTERN void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS(4_0);
という方法に目を向けました
しばらく試して、最後に成形しました.#pragma mark -
- (UIImage *)imageScaled:(float)sizeScale
{
CGSize size = CGSizeMake(self.size.width * sizeScale, self.size.height * sizeScale);
if([[UIScreen mainScreen] scale] == 3.0){
UIGraphicsBeginImageContextWithOptions(size, NO, 3.0);
}else if([[UIScreen mainScreen] scale] == 2.0){
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
}else{
UIGraphicsBeginImageContext(size);
}
[self drawInRect:CGRectMake(0, 0, self.size.width * sizeScale, self.size.height * sizeScale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
Prefectテストmmも満足でした()
もう一度この方法を見てくださいUIKIT_EXTERN void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS(4_0);
- (UIImage *)imageScaled:(float)sizeScale
{
CGSize size = CGSizeMake(self.size.width * sizeScale, self.size.height * sizeScale);
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, self.size.width * sizeScale, self.size.height * sizeScale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
// UIImage context
// The following methods will only return a 8-bit per channel context in the DeviceRGB color space.
// Any new bitmap drawing code is encouraged to use UIGraphicsImageRenderer in leiu of this API.
UIKIT_EXTERN void UIGraphicsBeginImageContext(CGSize size);
UIKIT_EXTERN void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN UIImage* __nullable UIGraphicsGetImageFromCurrentImageContext(void);
UIKIT_EXTERN void UIGraphicsEndImageContext(void);
#pragma mark -
- (UIImage *)imageScaled:(float)sizeScale
{
CGSize size = CGSizeMake(self.size.width * sizeScale, self.size.height * sizeScale);
if([[UIScreen mainScreen] scale] == 3.0){
UIGraphicsBeginImageContextWithOptions(size, NO, 3.0);
}else if([[UIScreen mainScreen] scale] == 2.0){
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
}else{
UIGraphicsBeginImageContext(size);
}
[self drawInRect:CGRectMake(0, 0, self.size.width * sizeScale, self.size.height * sizeScale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
最後に口を挟むと、私のこの方法は分類に書かれています.
直接来たいなら、そうしてもいいです.
- (UIImage *)scaleImage:(UIImage *)orginImage withScale:(float)sizeScale
{
CGSize size = CGSizeMake(orginImage.size.width * sizeScale, orginImage.size.height * sizeScale);
if([[UIScreen mainScreen] scale] == 3.0){
UIGraphicsBeginImageContextWithOptions(size, NO, 3.0);
}else if([[UIScreen mainScreen] scale] == 2.0){
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
}else{
UIGraphicsBeginImageContext(size);
}
[orginImage drawInRect:CGRectMake(0, 0, orginImage.size.width * sizeScale, orginImage.size.height * sizeScale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}