iOS開(kāi)發(fā) ,將多張圖片疊加繪制到一起

這種繪制是根據(jù)圖片的像素比例 等比例進(jìn)行繪制的,在選擇圖片和創(chuàng)建展示圖片的imageView 時(shí),注意查看尺寸 注:繪圖時(shí)使用 ?[UIScreen mainScreen].scale 可以是圖片更清晰?UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//這樣就不模糊了

//圖片上添加文字 詳細(xì)版

- (UIImage*)text:(NSString*)text addToView:(UIImage*)image{

//設(shè)置字體樣式

UIFont*font = [UIFont fontWithName:@"Arial-BoldItalicMT"size:100];

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];

[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 1)];

[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:100] range:NSMakeRange(0, text.length)];

//? ? CGSize textSize = [text sizeWithAttributes:dict];

CGSize textSize = [str size];

//繪制上下文

UIGraphicsBeginImageContext(image.size);

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//這樣就不模糊了

[image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];

//? ? int border =10;

CGRect re = {CGPointMake((image.size.width- textSize.width)/2, 200), textSize};

//? ? CGRect rect = CGRectMake(0, 0, image.size.width, 500);

//此方法必須寫在上下文才生效

[str drawInRect:re ];

UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

//修改圖片尺寸

- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{

// Create a graphics image context

UIGraphicsBeginImageContext(newSize);//這樣壓縮的圖片展示出來(lái)會(huì)很模糊

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//這樣就不模糊了

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//這樣就不模糊了

// Tell the old image to draw in this new context, with the desired

// new size

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

// End the context

UIGraphicsEndImageContext();

// Return the new image.

return newImage;

}

//圓角

- (UIImage *) getRadioImaeg:(NSString *)imageName1{

UIImage *image1 = [UIImage imageNamed:imageName1];

UIGraphicsBeginImageContextWithOptions(image1.size, 0, 0);

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGRect rect = CGRectMake(00, 0, image1.size.width, image1.size.width);

CGContextAddEllipseInRect(ctx, rect);

CGContextClip(ctx);

[image1 drawInRect:rect];

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;

}

//圖片疊加

- (UIImage *)addImage:(NSString *)imageName1 withImage:(NSString *)imageName2 {

UIImage *image1 = [UIImage imageNamed:imageName1];

UIImage *image2 = [self getRadioImaeg:@"333"];

UIGraphicsBeginImageContext(image1.size);

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//這樣就不模糊了

[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

[image2 drawInRect:CGRectMake((image1.size.width - image2.size.width)/2,(image1.size.height - image2.size.height)/2, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容