2012-07-11 25 views
15

Hola, intento capturar una vista y luego guardar como imagen en Photo Library, pero necesito crear una resolución personalizada para la imagen capturada, aquí está mi código, pero cuando la aplicación guarda las imágenes, la resolución es bajo !iOS: Guardar imagen con resolución personalizada

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

pero la resolución en el iPhone es: 320 x 320 y la retina es: 640 x 640

Le agradecería si me ayudas a solucionar este problema.

+1

[SupportingHiResScreens] (http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/SupportingHiResScreens/SupportingHiResScreens.html)> [ Creación de imágenes de mapa de bits de alta resolución mediante programación] (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

¿Qué tamaño de la imagen eliges de la Biblioteca de fotos? ¿Más grande o más pequeño que tu tamaño deseado (1435, 1435)? – holex

Respuesta

15

Tu código es bastante cercano. Lo que debe hacer es volver a procesar la captura de pantalla en la resolución personalizada. Modifiqué su código para hacer esto:

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); 

Tenga en cuenta que si la vista de captura no es cuadrada, la imagen se distorsionará. La imagen guardada siempre será cuadrada y 1435x1435 píxeles.

1

Primero obtener su imagen en el objeto UIImage. Cree su tamaño lo que quieras y el uso siguiente ..

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

Ahora se conseguirá destImage con la nueva resolución.

Hope esto es lo que busca :)

+1

¡las resoluciones 320 y 640 fueron la resolución de imagen capturada! Necesito 1435 res tanto en retina como en pantalla no retina. –

+3

que no se hará querido ... puedes hacerlo proporcionando que la imagen y el tamaño se arreglarán usando 'CGSizeMake (1425, 1435)'. Pero la calidad de tu imagen será muy pobre. –

+4

Solo para reiterar el comentario de Kapil: la pantalla solo tiene 320x480 o 640x960 píxeles para mostrar. Puede renderizarlo en una imagen mucho más grande, pero los píxeles de la pantalla simplemente se estirarán para llenar el tamaño de la imagen. No hay forma de crear una imagen de mayor resolución de lo que está en la pantalla. Dicho eso, si muestra una imagen de alta resolución reducida para adaptarse a la pantalla, o un pdf, PODRÍA renderizarlos en una imagen mucho más grande y obtener una alta producción de rez. –

7

echar un vistazo a this answer. El código incluye la rotación pero, no obstante, el que pregunta hizo la misma pregunta: "¿Cómo obtener una [...] imagen de un UIImageView con su resolución completa?"

contenido copiado (en caso de supresión o lo que sea):

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

Ir con esto puede solucionar su problema.

1

Puede utilizar lo siguiente:

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; 

}

Cuestiones relacionadas