2013-03-15 31 views

Trả lời

13

Đây có thể là quá mức cần thiết nhưng bạn chỉ cần chụp ảnh và tạo bối cảnh đồ họa ở độ phân giải mà bạn muốn, sau đó bạn có thể đặt tempImage làm UIImageView, ghi đè lên.

 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; 
+0

tại sao quá mức cần thiết ??? Ý tôi là, nó hoạt động, nhưng nó có bất kỳ hiệu ứng negativ nào không? – user1602687

+0

bạn nói bạn muốn một vài dòng. điều này giống như 10, lol. Không có tác động tiêu cực mặc dù! – ApolloSoftware

+0

Tôi đã thử điều này nhưng nó kết thúc mờ? – Lion789

1

Sử dụng lớp này (thêm mã để .h tập tin phù hợp)

#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(); 
} 

Đừng quên để thêm CoreGraphics frameawork.

0

tôi đã tìm thấy đoạn mã sau vào 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; 
} 

Tất cả bạn phải làm là cung cấp cho nó với i_width, và nó sẽ mở rộng nó cho phù hợp.

Tôi đã thêm một chút xoắn vào nó, để nếu ảnh ở chế độ ngang, ảnh sẽ được xoay sang dọc và sau đó thay đổi kích thước. Nếu bạn muốn nó được điều ngược lại (dọc sang ngang), thay đổi này:

if (sourceImage.size.width>sourceImage.size.height) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

này:

if (sourceImage.size.height>sourceImage.size.width) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

caveat: phương pháp quay của tôi không đi vào xem xét nếu hình ảnh được chỉ trái hay phải. Nói cách khác, nếu một hình ảnh được phong cảnh bằng lộn ngược, mã của tôi không thể nhận ra điều đó. Tôi hy vọng một người khác có thể làm sáng tỏ điều này mặc dù :)

Các vấn đề liên quan