可能な限り数行でUIImageのサイズを変更する方法はありますか?私は比率の気にしない、私はちょうど80x60に画像の解像度を設定したい。これはすべてXIIIでのUIImageの単純なサイズ変更
7
A
答えて
13
これは残酷かもしれませんが、イメージを取り込んで、必要な解像度でグラフィックスコンテキストを作成し、tempImageをUIImageViewとして設定して上書きすることができます。
UIImage *image = YourImageView.image;
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(80,60);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width = targetSize.width;
thumbnailRect.size.height = targetSize.height;
[image drawInRect:thumbnailRect];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
YourImageView.image = tempImage;
1
CoreGraphicsのframeaworkを追加することを忘れないでください
#import "UIImage+Resize.h"
@implementation UIImage (Resize)
- (UIImage *)resizedImage:(CGSize)bounds {
return [self resizedImage:bounds upScale:YES];
}
- (UIImage*)resizedImage:(CGSize)bounds upScale:(BOOL)upScale {
CGSize originalSize = self.size;
float xScale = bounds.width/originalSize.width;
float yScale = bounds.height/originalSize.height;
float scale = MIN(xScale, yScale);
if (!upScale) {
scale = MIN(scale, 1);
}
CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
1
- (void)resizeImage:(UIImage *)image
{
CGSize origImageSize = [image size];
CGRect imageRect = CGRectMake(0, 0, 80, 60);
float ratio = MAX(newRect.size.width/origImageSize.width,
newRect.size.height/origImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect
cornerRadius:5.0];
[path addClip];
CGRect imageRect;
imageRect.size.width = ratio * origImageSize.width;
imageRect.size.height = ratio * origImageSize.height;
imageRect.origin.x = (newRect.size.width - imageRect.size.width)/2.0;
imageRect.origin.y = (newRect.size.height - imageRect.size.height)/2.0;
[image drawInRect:imageRect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *data = UIImagePNGRepresentation(smallImage);
UIGraphicsEndImageContext();
}
(従ってファイルを.Hするためのコードを追加します)、このクラスを使用します。
0
私はthis pageに次のコードを見つけた:
- (UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width{
if (sourceImage.size.width>sourceImage.size.height) {
sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
scale: 1.0
orientation: UIImageOrientationRight];
}
float oldWidth = sourceImage.size.width;
float scaleFactor = i_width/oldWidth;
float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return newImage;
}
あなたがしなければならないのは、i_width
でそれを提供し、それはそれに応じて拡大縮小します。
私はそれに少しひねりを加えたので、ピクチャが横向きモードであれば、それは縦に回転され、サイズ変更されます。あなたはそれが反対(ランドスケープに肖像画)にしたい場合は、これを変更します。これに
if (sourceImage.size.width>sourceImage.size.height) {
sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
scale: 1.0
orientation: UIImageOrientationRight];
}
:
if (sourceImage.size.height>sourceImage.size.width) {
sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
scale: 1.0
orientation: UIImageOrientationRight];
}
CAVEAT:絵を指している場合は、私の回転方法は考慮していません左か右。言い換えれば、画像が横向きで横長である場合、私のコードはそれを認識できません。私は他の誰かがこれでもいくつかの光を当てることができるように願っています:)
関連する問題
- 1. UIScrollViewのUIImageのサイズ変更
- 2. UIPrintInteractionControllerのUIImageのサイズ変更
- 3. スレッドセーフUIImageサイズ変更?
- 4. マルチスレッドのuiimageサイズ変更エラー
- 5. UIImageのサイズ変更時のメモリリーク
- 6. iphoneカメラからUIimageのサイズを変更
- 7. UIImageのサイズを変更する
- 8. 単純なpythonリストの変更
- 9. UIImageをプログラム的にサイズ変更する
- 10. UIImageが予期せずサイズ変更
- 11. UIImage品質の低下なしにサイズ変更
- 12. UIImage */imageNamedで動作しない単純な代入
- 13. UIImageをメモリ制約の下でサイズ変更する
- 14. 元の比率でUIImageをサイズ変更する
- 15. マスクは任意にサイズ変更可能なUIImageとUIImageViewは
- 16. リストビューのテキストを変更する単純なリストビューでの色 - Android
- 17. 単純なSQL更新ステートメントでのエラー
- 18. 縦横比を維持しないでUIImage/UIImageViewのサイズを変更する
- 19. 単純なWPFグリッド線の色を変更できません
- 20. UIImageのサイズ変更に時間がかかりますか?
- 21. 3MbにuIImageのサイズを変更するには
- 22. UIImageのサイズを変更して保存する
- 23. UIImageのサイズが変更されています
- 24. uiimageピッカーコントローラで画像サイズを変更する方法
- 25. UIImageはハードエッジでサイズを変更します
- 26. 単純な背景の変更が動作しない
- 27. 単純なパーセンテージ更新
- 28. 単純な更新クエリのpostgresのデッドロック
- 29. Django - コードの単純な変更後のNoReverseMatch
- 30. のaddEventListenerは、単純なjsのは、スニペット変更イベント
なぜ過度の?つまり、動作しますが、ネガティブ効果がありますか? – user1602687
あなたはいくつかの行が必要だと言った。これは10、LOLのようなものです。しかし、悪影響はありません! – ApolloSoftware
私はこれを試しましたが、ぼやけてしまいますか? – Lion789