UIImage의 크기를 가능한 한 적은 줄로 조정하는 방법이 있습니까? 비율은 신경 쓰지 않고 이미지 해상도를 80x60으로 설정하고 싶습니다. 그게 전부예요XCODE에서 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
사용이 클래스 (따라 파일을있는 .h하는 코드를 추가)
#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();
}
가있는 CoreGraphics의 frameawork를 추가하는 것을 잊지 마십시오.
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];
}
주의 : 사진이 가리키는 경우 내 회전 방법은 고려하지 않는다 왼쪽 아니면 오른쪽. 즉, 이미지가 가로로 뒤집혀 있으면 내 코드에서이를 인식 할 수 없습니다. 나는 다른 누군가가 이것에 대한 약간의 빛을 비춰 줄 수 있기를 바랍니다 :)
관련 문제
- 1. Xcode에서 UIImage의 위치 고정
- 2. iOS - UIImage의 색조 조정
- 3. Xcode에서 인터페이스 빌더 크기 조정 처리가 변경되었습니다.
- 4. xcode에서 크기 조정 된 이미지를 취소하는 방법
- 5. UIImage의 파일 크기 줄이기?
- 6. 동적 크기 조정 단순 모달 클릭 한 내용에 따라
- 7. UIImage의 NSData 크기 변경 - 최대 메가 바이트 Mb 한계로 UIImage 압축 및 크기 조정
- 8. Xcode에서 xib에서 UIView 크기 조정 및 배치 4.3
- 9. UIImageView에서 크기가 조정 된 UIImage의 프레임에 액세스
- 10. 크기 조정 UIFont 크기
- 11. 태그가 지정된 UIImage 크기 조정
- 12. JqGrid 크기 조정 브라우저의 크기 조정 문제
- 13. Java에서 이미지 크기 조정. 크기 조정 방법
- 14. Java에서 이미지 크기 조정/크기 조정 방법
- 15. 내부 컨테이너의 글꼴 크기 조정/크기 조정
- 16. vb.net에서 이미지 조작/크기 조정/크기 조정
- 17. 배치 이미지 크기 조정/크기 조정
- 18. 크기 조정 div 윈도우에서의 너비 크기 조정
- 19. iframe 너비를 창 크기 조정 크기 조정
- 20. 윈도우 크기 조정 요소 크기 조정 jquery
- 21. 여러 비트 맵의 크기 조정/크기 조정
- 22. 윈도우에서 크기 조정 iframe 크기 조정
- 23. 진행보기 뒤집기 및 크기 조정
- 24. StoryBoard의 UITabBar 버튼 크기 조정
- 25. 크기 조정
- 26. 문서 디렉토리에 쓰기 전에 uiimage 크기 조정
- 27. UILabel의 내용을 동적으로 크기 조정
- 28. UIImage의 크기 (KB)를 얻는 방법
- 29. UIImage의 크기 속성에 액세스 할 때 EXC_BAD_ACCESS
- 30. Android : 크기 조정 후 크기 조정 및 크기 변경
왜 과다한 ??? 내 말은, 그것이 효과가 있지만 부정적인 영향을 미치나요? – user1602687
몇 줄을 원했다. 이것은 10, 롤과 같습니다. 아니 부정적인 영향을! – ApolloSoftware
나는 이걸 시도했지만 흐릿 해지는거야? – Lion789