iOS: Enregistrer l'image avec une résolution personnalisée

Salut je suis essayer de capturer une vue, puis save comme une image dans la photothèque, mais j'ai besoin de créer une résolution personnalisée pour l'image capturée, voici mon code, mais lorsque l'application enregistre les images, la résolution est faible!

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

mais la résolution dans l'iPhone est: 320 x 320 et la rétine est: 640 x 640

Je vous serais reconnaissant si vous m'aidiez à résoudre ce problème.

Votre code est assez proche. Ce que vous devez faire est de re-rendre la capture d'écran à la résolution personnalisée. J'ai modifié votre code pour le faire:

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

Notez que si la vue de capture n'est pas carrée, l'image sera déformée. L'image enregistrée sera toujours carrée et 1435×1435 pixels.

jetez un oeil à cette réponse . Le code inclut la rotation, mais le questionneur a néanless posé la même question: "Comment get une […] image à partir d'une UIImageView à sa pleine résolution?"

contenu copié (en cas de suppression ou autre):

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

Commencez par récupérer votre image dans l'object UIImage. Créez votre taille que vous voulez et utilisez le suivant ..

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

Maintenant, vous obtiendrez destImage avec une nouvelle résolution.

J'espère que c'est ce que vous cherchez 🙂

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

Aller avec cela peut résoudre votre problème.

Vous pouvez utiliser cela:

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 

Cela a fonctionné pour moi. J'ai essayé avec l'image que j'ai obtenu de Facebook SDK http://pulkitgoyal.in/resizing-high-resolution-images-on-ios-without-memory-issues/

Je pense que ALAssetRepresentation peut vous aider.

 CGSize sizePic = CGSizeMake(320, 460); UIGraphicsBeginImageContext(sizePic); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *imagePic = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(imagePic, nil, nil, nil); 
 #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; 

}