क्या कोई तरीका है कि यूआईएममेज को यथासंभव कम से कम लाइनों में कैसे बदला जाए? मुझे अनुपात का कोई फर्क नहीं पड़ता, मैं सिर्फ छवि संकल्प को 80x60 पर सेट करना चाहता हूं। यह सबXCODE में UIImage का सरल आकार बदलने
उत्तर
यह अधिक हो सकता है लेकिन, आप बस अपनी छवि ले सकते हैं, और उस संकल्प पर एक ग्राफिक्स संदर्भ बना सकते हैं, फिर आप 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;
इस वर्ग का उपयोग करें (तदनुसार फ़ाइल ज पर कोड शामिल)
#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 जोड़ने के लिए मत भूलना।
मैं 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. UIImage का आकार बदलने का कौन सा तरीका बेहतर है और क्यों?
- 2. UIImage imageNamed: नीले संदर्भित Xcode-folder
- 3. FullCalendar - आकार बदलने खिड़की पर कैलेंडर का आकार बदलने के
- 4. मास्क मनमाने ढंग से आकार बदलने योग्य UIImage साथ UIImageView आकार मुखौटा
- 5. सी # छवि का आकार बदलने का आकार फ़ाइल आकार
- 6. QLabel आकार बदलने का पाठ
- 7. विंडोज प्रपत्र का आकार बदलने में अक्षम
- 8. सी ++ में आकार खींचने का सरल तरीका?
- 9. ggmap मानचित्र का आकार बदलने
- 10. UIImage
- 11. UIImage हार्ड किनारों के साथ आकार बदलें
- 12. बदलने स्रोत फ़ाइलों XCode 4
- 13. UIImage गुणवत्ता के नुकसान के बिना आकार
- 14. UIImage आकार को कैसे कम करें ...?
- 15. jQuery आकार बदलने वाले तत्व
- 16. जावाएफएक्स में स्टेज का आकार बदलने का तरीका कैसे सुनें?
- 17. बदलने Highcharts फ़ॉन्ट आकार
- 18. UIImage से CIImage - आकार आधा है?
- 19. आकार बदलने वाले कर्सर
- 20. एसीई संपादक का आकार बदलने योग्य
- 21. एनएसविंडो का आकार बदलने वाला संकेतक
- 22. का आकार बदलने और heatmap.2
- 23. Iframe आकार बदलने का ट्रिगर नहीं करता
- 24. ImageMagick बैच आकार बदलने का प्रदर्शन
- 25. Css का आकार बदलने के न्यूनतम-चौड़ाई
- 26. विंडो आकार बदलने पर ग्रिडपेनल ऑटो का आकार
- 27. विंडो आकार बदलने पर कलरबॉक्स का आकार बदलें
- 28. jQuery यूआई आकार बदलने योग्य आग खिड़की का आकार बदलें
- 29. स्वचालित रूप से आकार बदलने वाले सबव्यू का आकार बदलना
- 30. उपयोगकर्ताओं को विंडो/फॉर्म आकार का आकार बदलने से रोकें
क्यों ओवरकिल ??? मेरा मतलब है, यह काम करता है, लेकिन क्या इसका कोई नकारात्मक प्रभाव पड़ता है? – user1602687
आपने कहा था कि आप कुछ लाइनें चाहते थे। यह 10 की तरह है, लॉल। हालांकि कोई नकारात्मक प्रभाव नहीं! – ApolloSoftware
मैंने कोशिश की लेकिन यह धुंधला हो गया? – Lion789