'da UIImage'ın yeniden boyutlandırılması UIImage'ın mümkün olduğunca az satırda nasıl yeniden boyutlandırılacağı var mı? Oranın farkında değil, sadece görüntü çözünürlüğünü 80x60 olarak ayarlamak istiyorum. Bu tümXCODE
XCODE
cevap
Bu aşırı bir şekilde olabilir, ancak görüntünüzü alabilir ve istediğiniz çözünürlükte bir grafik bağlamı oluşturabilir, ardından tempImage'ı UIImageView olarak ayarlayabilir ve üzerine yazabilirsiniz.
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;
kullanın bu sınıf (buna göre dosyayı .h kodu ekleyin)
#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;
}
- (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 eklemeyi unutmayın.
Ben this page üzerine aşağıdaki kodu buldum:
- (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;
}
yapmanız gereken tek şey i_width
ile bunu sağlamak olduğunu ve buna göre ölçeklenecektir.
Resimde manzara modundaysa, dikey olarak döndürülüp yeniden boyutlandırılacak şekilde küçük bir bükülme ekledim. Eğer tam tersi (peyzaja dikey) olmasını istiyorsanız, bunu değiştirmek: Buna
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];
}
Ikaz: resim işaret eğer benim dönen yöntem dikkate almaz sol veya sağ. Başka bir deyişle, bir resim baş aşağı yatay ise, kodum bunu tanıyamaz. Umarım başka biri bu konuda biraz ışık tutabilir :)
- 1. Xcode
- 2. Xcode
- 3. xcode
- 4. Xcode
- 5. Xcode
- 6. Xcode
- 7. Xcode
- 8. Xcode
- 9. Xcode
- 10. XCode
- 11. Xcode
- 12. XCode
- 13. Xcode
- 14. Xcode
- 15. XCode
- 16. Xcode
- 17. Xcode
- 18. XCode,
- 19. CMake oluşturulan Xcode projesi, Xcode
- 20. Xcode birleştirme hedefi nasıl Xcode 6.1.1
- 21. Xcode en son sürümüne sahip, Xcode
- 22. XCode 4 ve XCode 3.2.6 yan yana
- 23. Xcode benim XLIFF dosya verirken Xcode
- 24. Xcode 4
- 25. nasıl Xcode
- 26. Hata: Xcode
- 27. Xcode 4
- 28. Xcode Playground
- 29. Xcode (iPad)
- 30. Uygulamayı Xcode
neden overkill ??? Demek istediğim, çalışıyor, ama olumsuz bir etkisi var mı? – user1602687
Birkaç satır istediğini söyledin. Bu 10 gibi, lol. Olsa da olumsuz bir etki yok! – ApolloSoftware
Bunu denedim ama bulanıklaşıyor mu? – Lion789