2012-07-11 36 views
15

Xin chào Tôi cố gắng chụp một khung nhìn rồi lưu dưới dạng hình ảnh vào Thư viện ảnh, nhưng tôi cần tạo độ phân giải tùy chỉnh cho hình ảnh đã chụp, đây là mã của tôi nhưng khi ứng dụng lưu hình ảnh độ phân giải chậm !iOS: Lưu hình ảnh với độ phân giải tùy chỉnh

UIGraphicsBeginImageContextWithOptions(self.captureView.bounds.size, self.captureView.opaque, 0.0); 

[self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); 

CGRect cropRect = CGRectMake(0 ,0 ,1435 ,1435); 
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect); 
CGImageRelease(imageRef); 

UIImageWriteToSavedPhotosAlbum(screenshot , nil, nil, nil); 

UIGraphicsEndImageContext(); 

nhưng độ phân giải trong iPhone là: 320 x 320 và võng mạc là: 640 x 640

tôi sẽ biết ơn nếu bạn giúp tôi khắc phục vấn đề này.

+1

[SupportingHiResScreens] (http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/SupportingHiResScreens/SupportingHiResScreens.html)> [ Tạo hình ảnh bitmap có độ phân giải cao theo lập trình] (http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/SupportingHiResScreens/SupportingHiResScreens.html#//apple_ref/doc/uid/TP40010156-CH15- SW9) – Bala

+1

http://stackoverflow.com/a/613576/1059705 – Bala

+1

kích thước hình ảnh nào bạn chọn từ Thư viện ảnh? Lớn hơn hoặc nhỏ hơn kích thước mong muốn của bạn (1435, 1435)? – holex

Trả lời

15

Mã của bạn khá gần. Những gì bạn cần làm là tái render ảnh chụp màn hình ở độ phân giải tùy chỉnh. Tôi đã sửa đổi mã của bạn để làm điều này:

UIView* captureView = self.view; 

/* Capture the screen shoot at native resolution */ 
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0); 
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

/* Render the screen shot at custom resolution */ 
CGRect cropRect = CGRectMake(0 ,0 ,1435 ,1435); 
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f); 
[screenshot drawInRect:cropRect]; 
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

/* Save to the photo album */ 
UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil); 

Lưu ý rằng nếu chế độ xem chụp không vuông thì hình ảnh sẽ bị méo. Hình ảnh đã lưu sẽ luôn là hình vuông và 1435x1435 pixel.

1

Đầu tiên tải hình ảnh của bạn trong đối tượng UIImage. Tạo kích thước của bạn những gì mà bạn muốn và sử dụng sau ..

UIImage *image = // you image; 
CGSize size; 
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && 
([UIScreen mainScreen].scale == 2.0)) { 

    // RETINA DISPLAY 
     size = CGSizeMake(640, 640); 
} 
else { 
    // Non Ratina device 
     size = CGSizeMake(320, 320); 
} 

UIGraphicsBeginImageContext(size); 
[image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext(); 

Bây giờ bạn sẽ nhận được destImage với độ phân giải mới.

Hy vọng điều này là những gì bạn đang tìm kiếm :)

+1

độ phân giải 320 và 640 là độ phân giải hình ảnh đã chụp! Tôi cần 1435 res cả trong võng mạc và màn hình không võng mạc. –

+3

mà sẽ không được thực hiện thân yêu .. bạn có thể làm điều đó bằng cách cung cấp hình ảnh và kích thước sẽ được cố định bằng cách sử dụng 'CGSizeMake (1425, 1435)'. Nhưng chất lượng hình ảnh của bạn sẽ rất kém. –

+4

Chỉ cần nhắc lại nhận xét của Kapil - màn hình chỉ có 320x480 hoặc 640x960 pixel để hiển thị. Bạn có thể hiển thị hình ảnh đó thành một hình ảnh lớn hơn nhiều, nhưng các pixel của màn hình sẽ chỉ được kéo dài để lấp đầy kích thước của hình ảnh. Không có cách nào để tạo ra hình ảnh có độ phân giải cao hơn trên màn hình. Điều đó nói rằng, nếu bạn đang hiển thị một hình ảnh thu nhỏ cao để phù hợp với màn hình, hoặc một pdf, bạn có thể làm cho những người vào một hình ảnh lớn hơn nhiều và có được một đầu ra rez cao. –

7

có một cái nhìn tại this answer. Mã này bao gồm tính năng xoay nhưng dù sao người hỏi cũng hỏi cùng một câu hỏi: "Cách lấy [[]] hình ảnh từ một UIImageView ở độ phân giải đầy đủ của nó?"

nội dung sao chép (trong trường hợp xóa hoặc bất cứ điều gì):

- (UIImage *)capturedView 
{ 
    float imageScale = sqrtf(powf(self.captureView.transform.a, 2.f) + powf(self.captureView.transform.c, 2.f));  
    CGFloat widthScale = self.captureView.bounds.size.width/self.captureView.image.size.width; 
    CGFloat heightScale = self.captureView.bounds.size.height/self.captureView.image.size.height; 
    float contentScale = MIN(widthScale, heightScale); 
    float effectiveScale = imageScale * contentScale; 

    CGSize captureSize = CGSizeMake(enclosingView.bounds.size.width/effectiveScale, enclosingView.bounds.size.height/effectiveScale); 

    NSLog(@"effectiveScale = %0.2f, captureSize = %@", effectiveScale, NSStringFromCGSize(captureSize)); 

    UIGraphicsBeginImageContextWithOptions(captureSize, YES, 0.0);   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextScaleCTM(context, 1/effectiveScale, 1/effectiveScale); 
    [enclosingView.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return img; 
} 
1
-(UIImage*)processImageRect:(UIImage*)image:(CGSize)sizeToForm { 
    // Draw image1 
    UIGraphicsBeginImageContext(CGSizeMake(sizeToForm.width, sizeToForm.height)); 
    [image drawInRect:CGRectMake(0.0, 0.0, sizeToForm.width, sizeToForm.height)]; 
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 

    return resultingImage; 
} 

Go với điều này có thể giải quyết vấn đề của bạn.

1

Bạn có thể sử dụng:

UIImageExtras.h

@interface UIImage (Extras) 
-(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize; 
@end 

UIImageExtras.m

#import "UIImageExtras.h" 

@implementation UIImage (Extras) 

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 
{ 
//Image de base 
UIImage *sourceImage = self; 
//Image redimenssionnée 
UIImage *newImage = nil; 

//Taille de l'image de base 
CGSize imageSize = sourceImage.size; 
//Longueur et largeur 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 

//Dimension désirée 
CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 

//Echelle... 
CGFloat scaleFactor = 0.0; 
CGFloat scaledWidth = targetWidth; 
CGFloat scaledHeight = targetHeight; 
CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

//Si taille des image est différentes on redimensionne de facon proportionnelle 
if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
{ 
    CGFloat widthFactor = targetWidth/width; 
    CGFloat heightFactor = targetHeight/height; 

    if (widthFactor > heightFactor) 
     scaleFactor = widthFactor; // scale to fit height 
    else 
     scaleFactor = heightFactor; // scale to fit width 
    scaledWidth = width * scaleFactor; 
    scaledHeight = height * scaleFactor; 

    //Centre l'image 
    if (widthFactor > heightFactor) 
    { 
     thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    } 
    else if (widthFactor < heightFactor) 
     { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    }  

    UIGraphicsBeginImageContext(targetSize); 

    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    if(newImage == nil) 
     NSLog(@"could not scale image"); 

    UIGraphicsEndImageContext(); 

    return newImage; 
} 
@end 
0
CGSize sizePic = CGSizeMake(320, 460); 
    UIGraphicsBeginImageContext(sizePic); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *imagePic = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(imagePic, nil, nil, nil); 
0
#import <ImageIO/ImageIO.h> 
#import <MobileCoreServices/MobileCoreServices.h> 

+ (UIImage *)resizeImage:(UIImage *)image toResolution:(int)resolution { 
NSData *imageData = UIImagePNGRepresentation(image); 
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL); 
CFDictionaryRef options = (__bridge CFDictionaryRef) @{ 
                 (id) kCGImageSourceCreateThumbnailWithTransform : @YES, 
                 (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES, 
                 (id) kCGImageSourceThumbnailMaxPixelSize : @(resolution) 
                 }; 
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); 
CFRelease(src); 
UIImage *img = [[UIImage alloc]initWithCGImage:thumbnail]; 
return img; 

}

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